Python Format String

December 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}"
This work is licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License.