前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python全栈开发《29.字符串格式化的三种方式之格式化函数-format、新格式化方案:f-strings》

python全栈开发《29.字符串格式化的三种方式之格式化函数-format、新格式化方案:f-strings》

作者头像
清菡
发布2024-10-15 18:43:16
920
发布2024-10-15 18:43:16
举报
文章被收录于专栏:清菡软件测试

1.字符串格式化函数-format

  • 1)string.format函数用来格式化字符串。
  • 2)使用format的字符串主体使用{}大括号来替代格式符。
  • 3)完整用法:string.format(data,data,data...)
代码语言:javascript
复制
print('hello {0},今天看起来气色{1}'.format('小编','不错'))

运行结果:

代码语言:javascript
复制
/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:

代码语言:javascript
复制
# 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))

运行结果:

代码语言:javascript
复制
/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:

代码语言:javascript
复制
# 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))

运行结果:

代码语言:javascript
复制
/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:

代码语言:javascript
复制
# 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))

运行结果:

代码语言:javascript
复制
/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

2.python3.6加入的新格式化方案:f-strings

  • 定义一个变量。
  • 字符串前加f符号。
  • 需要格式化的位置使用{变量名}。
代码语言:javascript
复制
# 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)

代码语言:javascript
复制
/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

这种格式化方法,一定要提前定义好变量名,否则是无法使用的。

3.哪种格式化方法更好?

推荐使用format函数,使用format函数不需要提前定义好变量名,也比第一种方法:根据类型定义的格式化,更简单。

代码语言:javascript
复制
# 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']))

运行结果:

代码语言:javascript
复制
/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
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2024-08-18,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 清菡软件测试 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1.字符串格式化函数-format
  • 2.python3.6加入的新格式化方案:f-strings
  • 3.哪种格式化方法更好?
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档