There are three ways to format strings :
1、 Use placeholders % format , Original version
2、 Use str.format() format ,python2.6 Version to introduce
3、f-strings expression , Can parse any type of data , Render at run time , Performance ratio %,.format() Better .python3.6 Version to introduce
name = 'peter'
age = 20
# %s Formatted string ,%d Formatted integer
print('%s is %d years old' % (name, age))
# str.format() Three ways of writing , The parameter type is not limited , Number
print('{} is {} years old'.format(name, age))
print('{1} is {1} years old'.format(name, age))
print('{name} is {name} years old'.format(name='test', age=22))
# f-string Basic usage of expressions
print(f'{name} is {age} years old')
print(F'{name} is {age} years old')
# Analytic expression
print(f'{1 + 2}')
print(f'{2 * 4}')
# Parsing built-in functions
print(f'{name.upper()}')
# Attribute methods for parsing basic data types
list = ['hello', 'world']
print(f'{list[0]}')
------------------------------------------------
Running results :
peter is 20 years old
peter is 20 years old
20 is 20 years old
test is test years old
peter is 20 years old
peter is 20 years old
3
8
PETER
hello