python-字符串中含有变量的表示方法

 

name='liming'
age = 20

#1 字符串拼接,变量age必须是str类型
print('he is '+str(age)+' years old')
执行结果
he is 20 years old

#2 %s代表变量,
print('he is %s years old' % age)
执行结果
he is 20 years old

#3 format
print('{} is {} years old'.format(name,age))
print('{0} is {1} years old'.format(name,age))
print('{name_} is {age_} years old'.format(name_=name,age_=age))
执行结果
liming is 20 years old
liming is 20 years old
liming is 20 years old

 

posted @ 2019-08-21 16:42  Wsnan  阅读(3220)  评论(0编辑  收藏  举报