print('hello {0},今天看起来气色{1}'.format('小编','不错'))
运行结果:
/Users/llq/PycharmProjects/pythonlearn/pythonlearn/.venv/bin/python /Users/llq/PycharmProjects/pythonlearn/pythonlearn1/1.py
hello 小编,今天看起来气色不错
进程已结束,退出代码为 0
0和1分别代表了format中的顺序。0代表小编,1代表不错。
不需要在{}中添加位置的数字:0,1这样的,添加了位置数字也是没有错的:
例1:
# coding:utf-8
info = 'my name is %s,my age is %s'
name_01 = '小编'
age_01 = 10
name_02 = 'dewei'
age_02 = 33
print(info % (name_01,age_01))
print(info % (name_02,age_02))
message = '您好,今天是%s,您的手机号码:%s 已经欠费了,请尽快充值'
print(message % ('星期一',123456789))
print(message % (1234567,'星期2'))
print(message)
books =['python','django','flask']
info_2 = 'my name is %s,my age is %s,my book is %s'
print(info_2 % (name_01,age_01,books))
dict_01 = {'a':'a','b':'b'}
print('dict is %s' % dict_01)
info_3 = 'my name is {},my age is {}'
print(info_3.format(name_02,age_02))
运行结果:
/Users/llq/PycharmProjects/pythonlearn/pythonlearn/.venv/bin/python /Users/llq/PycharmProjects/pythonlearn/pythonlearn1/format.py
my name is 小编,my age is 10
my name is dewei,my age is 33
您好,今天是星期一,您的手机号码:123456789 已经欠费了,请尽快充值
您好,今天是1234567,您的手机号码:星期2 已经欠费了,请尽快充值
您好,今天是%s,您的手机号码:%s 已经欠费了,请尽快充值
my name is 小编,my age is 10,my book is ['python', 'django', 'flask']
dict is {'a': 'a', 'b': 'b'}
my name is dewei,my age is 33
进程已结束,退出代码为 0
例2:
# coding:utf-8
info = 'my name is %s,my age is %s'
name_01 = '小编'
age_01 = 10
name_02 = 'dewei'
age_02 = 33
print(info % (name_01,age_01))
print(info % (name_02,age_02))
message = '您好,今天是%s,您的手机号码:%s 已经欠费了,请尽快充值'
print(message % ('星期一',123456789))
print(message % (1234567,'星期2'))
print(message)
books =['python','django','flask']
info_2 = 'my name is %s,my age is %s,my book is %s'
print(info_2 % (name_01,age_01,books))
dict_01 = {'a':'a','b':'b'}
print('dict is %s' % dict_01)
info_3 = 'my name is {0},my age is {1},my book is {2}'
print(info_3.format(name_02,age_02,books))
运行结果:
/Users/llq/PycharmProjects/pythonlearn/pythonlearn/.venv/bin/python /Users/llq/PycharmProjects/pythonlearn/pythonlearn1/format.py
my name is 小编,my age is 10
my name is dewei,my age is 33
您好,今天是星期一,您的手机号码:123456789 已经欠费了,请尽快充值
您好,今天是1234567,您的手机号码:星期2 已经欠费了,请尽快充值
您好,今天是%s,您的手机号码:%s 已经欠费了,请尽快充值
my name is 小编,my age is 10,my book is ['python', 'django', 'flask']
dict is {'a': 'a', 'b': 'b'}
my name is dewei,my age is 33,my book is ['python', 'django', 'flask']
进程已结束,退出代码为 0
如果format中的数量和格式符不一致,就会报错:
例3:
# coding:utf-8
info = 'my name is %s,my age is %s'
name_01 = '小编'
age_01 = 10
name_02 = 'dewei'
age_02 = 33
print(info % (name_01,age_01))
print(info % (name_02,age_02))
message = '您好,今天是%s,您的手机号码:%s 已经欠费了,请尽快充值'
print(message % ('星期一',123456789))
print(message % (1234567,'星期2'))
print(message)
books =['python','django','flask']
info_2 = 'my name is %s,my age is %s,my book is %s'
print(info_2 % (name_01,age_01,books))
dict_01 = {'a':'a','b':'b'}
print('dict is %s' % dict_01)
info_3 = 'my name is {0},my age is {1},my book is {2}'
print(info_3.format(name_02,age_02))
运行结果:
/Users/llq/PycharmProjects/pythonlearn/pythonlearn/.venv/bin/python /Users/llq/PycharmProjects/pythonlearn/pythonlearn1/format.py
my name is 小编,my age is 10
my name is dewei,my age is 33
您好,今天是星期一,您的手机号码:123456789 已经欠费了,请尽快充值
您好,今天是1234567,您的手机号码:星期2 已经欠费了,请尽快充值
您好,今天是%s,您的手机号码:%s 已经欠费了,请尽快充值
my name is 小编,my age is 10,my book is ['python', 'django', 'flask']
dict is {'a': 'a', 'b': 'b'}
Traceback (most recent call last):
File "/Users/llq/PycharmProjects/pythonlearn/pythonlearn1/format.py", line 25, in <module>
print(info_3.format(name_02,age_02))
IndexError: Replacement index 2 out of range for positional args tuple
进程已结束,退出代码为 1
# coding:utf-8
info = 'my name is %s,my age is %s'
name_01 = '小编'
age_01 = 10
name_02 = 'dewei'
age_02 = 33
print(info % (name_01,age_01))
print(info % (name_02,age_02))
message = '您好,今天是%s,您的手机号码:%s 已经欠费了,请尽快充值'
print(message % ('星期一',123456789))
print(message % (1234567,'星期2'))
print(message)
books =['python','django','flask']
info_2 = 'my name is %s,my age is %s,my book is %s'
print(info_2 % (name_01,age_01,books))
dict_01 = {'a':'a','b':'b'}
print('dict is %s' % dict_01)
info_3 = 'my name is {0},my age is {1},my book is {2}'
print(info_3.format(name_02,age_02,books))
info_4 = f'my name is {name_01},my age is {age_02}'
print(info_4)
/Users/llq/PycharmProjects/pythonlearn/pythonlearn/.venv/bin/python /Users/llq/PycharmProjects/pythonlearn/pythonlearn1/format.py
my name is 小编,my age is 10
my name is dewei,my age is 33
您好,今天是星期一,您的手机号码:123456789 已经欠费了,请尽快充值
您好,今天是1234567,您的手机号码:星期2 已经欠费了,请尽快充值
您好,今天是%s,您的手机号码:%s 已经欠费了,请尽快充值
my name is 小编,my age is 10,my book is ['python', 'django', 'flask']
dict is {'a': 'a', 'b': 'b'}
my name is dewei,my age is 33,my book is ['python', 'django', 'flask']
my name is 小编,my age is 33
进程已结束,退出代码为 0
这种格式化方法,一定要提前定义好变量名,否则是无法使用的。
推荐使用format函数,使用format函数不需要提前定义好变量名,也比第一种方法:根据类型定义的格式化,更简单。
# coding:utf-8
info = 'my name is %s,my age is %s'
name_01 = '小编'
age_01 = 10
name_02 = 'dewei'
age_02 = 33
print(info % (name_01,age_01))
print(info % (name_02,age_02))
message = '您好,今天是%s,您的手机号码:%s 已经欠费了,请尽快充值'
print(message % ('星期一',123456789))
print(message % (1234567,'星期2'))
print(message)
books =['python','django','flask']
info_2 = 'my name is %s,my age is %s,my book is %s'
print(info_2 % (name_01,age_01,books))
dict_01 = {'a':'a','b':'b'}
print('dict is %s' % dict_01)
info_3 = 'my name is {0},my age is {1},my book is {2}'
print(info_3.format(name_02,age_02,books))
info_4 = f'my name is {name_01},my age is {age_02}'
print(info_4)
print(info_3.format('dewei',33,['python']))
运行结果:
/Users/llq/PycharmProjects/pythonlearn/pythonlearn/.venv/bin/python /Users/llq/PycharmProjects/pythonlearn/pythonlearn1/format.py
my name is 小编,my age is 10
my name is dewei,my age is 33
您好,今天是星期一,您的手机号码:123456789 已经欠费了,请尽快充值
您好,今天是1234567,您的手机号码:星期2 已经欠费了,请尽快充值
您好,今天是%s,您的手机号码:%s 已经欠费了,请尽快充值
my name is 小编,my age is 10,my book is ['python', 'django', 'flask']
dict is {'a': 'a', 'b': 'b'}
my name is dewei,my age is 33,my book is ['python', 'django', 'flask']
my name is 小编,my age is 33
my name is dewei,my age is 33,my book is ['python']
进程已结束,退出代码为 0