1 +
name='lisi'
print('you name'+name)
2%格式化输出 类是与C
name='lisi'
age=25
price=345.78
print('name is%s'%(name))
print('i am %d'%(age)+'year')
3 format函数
变量较多的情况下使用
"%s%d"%("a",1)
"{}{}".format("a",1)
三种方式
(1)不带编号,即“{}”
(2)带数字编号,可调换顺序,即“{1}”、“{2}”
(3)带关键字,即“{a}”、“{tom}”
>>> print('{} {}'.format('hello','world')) # 不带字段
hello world
>>> print('{0} {1}'.format('hello','world')) # 带数字编号
hello world
>>> print('{0} {1} {0}'.format('hello','world')) # 打乱顺序
hello world hello
>>> print('{1} {1} {0}'.format('hello','world'))
world world hello
>>> print('{a} {tom} {a}'.format(tom='hello',a='world')) # 带关键字
world hello world
name = 'zhangsan'
age = 25
price = 4500.225
info = 'my name is {my_name},i am {my_age} years old,my price is {my_price}'\
.format(my_name=name,my_age=age,my_price=price)
print(info)
结果为:
my name is zhangsan,i am 25 years old,my price is 4500.225