info = { "name": "John", "age": 35}
# old style
print("The name is %(name)s and the age is %(age)d" % info)
# new style
print("The name is {name} and the age is {age}".format_map(info))
>>> character_name = 'john'
>>> character_age = 35
>>> 'The man {} was {} years old'.format(character_name,character_age)
'The man john was 35 years old'
Each {} in the string is a placeholder which can be replaced with it's corresponding variable within .format()
Howver you can convert the integer to a string to concatenate or you can use string formatting.
There's two different kinds of string formatting.
The 'old style formatting', that's more similiar to the printf formatting style of the C language
and the new formatting style which is more powerful, but a little more to type for simple cases.
Old style with the '%' operator
New style formatting with the .format() method
Documentation for new style formatting:
https://docs.python.org/3/library/string.html?highlight=string#string.ascii_uppercase
and examples at
https://docs.python.org/3/library/string.html?highlight=string#formatexamples
Documentation for old style formatting:
https://docs.python.org/3/library/stdtypes.html#printf-style-string-formatting
Open in new window