syntax.us Let the syntax do the talking
Blog Contact Posts Questions Tags Hire Me

Question:
In Python, how do I embed variables within a string?

Ruby offers me an elegant way to embed variables within a string:
myvariable = "World"
mystring   = "Hello #{myvariable}"
p(mystring)
The obvious way to glue together strings in Python is to use a plus-operator:
myvariable = "World"
mystring   = "Hello " + myvariable
print(mystring)
In Python, another way to do this is displayed below:
myvariable = "World"
mystring   = "Hello {0}".format(myvariable)
print(mystring)
I like the Ruby way.
syntax.us Let the syntax do the talking
Blog Contact Posts Questions Tags Hire Me