Python Format String

Dec 22, 2018
Use string.format or f-string

For python 2.x, use string.format.

name = "Desmond"city = "Paris""Hi {}, welcome to {}".format(name, city)
"Hi {0}, welcome to {1}".format(name, city)
"Hi {name}, welcome to {city}".format(name=name, city=city)

The old way

"Hi %s, welcome to %s" % (name, city)

For python 3.6, use f-String

name = "Desmond"city = "Paris"f"Hi {name}, welcome to {city}"

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.