Skip to content Skip to sidebar Skip to footer

Return Formatted String In Python

I have a string: testString = ''' My name is %s and I am %s years old and I live in %s''' I have code that finds these three strings that I want to input into testString. How do I

Solution 1:

this is the version using %:

print""" My name is %s
and I am %s years old
and I live in %s""" % ('tim', 6, 'new york')

and this my preferred version:

testString = """ My name is {}
and I am {} years old
and I live in {}""".format(string1, string2, string3)

you may want to read https://docs.python.org/3.4/library/string.html#string-formatting .

Post a Comment for "Return Formatted String In Python"