章节
问答
课签
笔记
评论
占位
占位

Python的字符串format

字符串是Python程序重要的数据类型,到目前为止,我们输出的字符串的内容都是固定的,但有时候通过字符串输出的内容不是固定的,这个时候需要使用format来处理字符串,输出不固定的内容。
字符串format由两个部分组成,字符串模板和模板数据内容组成,通过大括号{},就可以把模板数据内容嵌到字符串模板对应的位置。

# 字符串模板
template = 'Hello {}'
# 模板数据内容
world = 'World'
result = template.format(world)
print(result) # ==> Hello World

如果模板中{}比较多,则容易错乱,那么在format的时候也可以指定模板数据内容的顺序。

# 指定顺序
template = 'Hello {0}, Hello {1}, Hello {2}, Hello {3}.'
result = template.format('World', 'China', 'Beijing', 'imooc')
print(result) # ==> Hello World, Hello China, Hello Beijing, Hello imooc.
# 调整顺序
template = 'Hello {3}, Hello {2}, Hello {1}, Hello {0}.'
result = template.format('World', 'China', 'Beijing', 'imooc')
print(result) # ==> Hello imooc, Hello Beijing, Hello China, Hello World.

除了使用顺序,还可以指定对应的名字,使得在format过程更加清晰。

# 指定{}的名字w,c,b,i
template = 'Hello {w}, Hello {c}, Hello {b}, Hello {i}.'
world = 'World'
china = 'China'
beijing = 'Beijing'
imooc = 'imooc'
# 指定名字对应的模板数据内容
result = template.format(w = world, c = china, b = beijing, i = imooc)
print(result) # ==> Hello World, Hello China, Hello Beijing, Hello imooc.

任务

请使用两种format的方式打印字符串Life is short, you need Python

?不会了怎么办

参考答案:

print('Life is short, you need {}'.format('Python'))
print('Life is short, you need {launguage}'.format( launguage = 'Python'))
||
1
2
# Enter a code
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
提交
重置代码
||

请验证,完成请求

由于请求次数过多,请先验证,完成再次请求

加群二维码

打开微信扫码自动绑定

您还未绑定服务号

绑定后可得到

  • · 粉丝专属优惠福利
  • · 大咖直播交流干货
  • · 课程更新,问题答复提醒
  • · 账号支付安全提醒

收藏课程后,能更快找到我哦~

使用 Ctrl+D 可将课程添加到书签

邀请您关注公众号
关注后,及时获悉本课程动态

举报

0/150
提交
取消
全部 精华 我要发布

最新回答 / 翎栋
# -*- coding: utf-8 -*-
# 加上上面这个注释就可以带有中文的注释了b = "Python"print("Life is short, you need {c}".format(c=b))print("Life is short, you need {0}".format("Python"))# no chinese world or 

最新回答 / 慕沐7315667
a = 'AABCDEFGHHIJ'b = a[1:9] //起始位置是1,结束位置在8,要取到9print(b)

最新回答 / 慕少1536510
shunyi=template=result = template.format(=,=,=,=shunyi,=)
(result)

最赞回答 / 慕田峪9313994
去掉大括号里的空格,再试试看

最新回答 / 天堂没有神
template1 = 'Life is {},'template2 = 'you need {a}'k423 = 'python'print(template1.format('short'),template2.format(a=k423))这样就可以

最新回答 / qq_慕工程7590247
result = template.format(w=w , c=c , b=b, i=i )这一行要这样写,w=w,第一个w指template = 'Hello {w}, Hello {c}, Hello {b}, Hello {i}.'这里定义的形参,第二个指 w = 'World'这里定义的实参学了函数就知道了这个报错就是编译器找不到你定义的实参

最赞回答 / weixin_慕斯0515414
template='{} {} {} ,{} {} {}'resulr=template.format('Life','is','short','you','need','pyton')print(resulr)

最新回答 / 魏忘尘
这个没有内含ASCII码,你换个软件用就是

最赞回答 / weixin_慕村6176323
.format 的意思是指可以任意替代某一个位置例:第一种<...code...>第二种<...code...>注:{}代表空字典常常与.format连用

最新回答 / 慕仙5161630
你把每行后面的分号去掉试试看

最赞回答 / 慕运维3303162
第四五种方法,你把life 和 need 后面都加个空格再{},就可以了吧。我感觉是:你目前是把“life”给了is,但是目标要求的是把“life+空格”这个给is ,这样is前面就有空格了。——同初学有问题可探讨,不喜勿喷^_^

最赞回答 / 慕村0394702
假如电子书中有1000个‘美好’这个词语,现在想把这个词换成‘漂亮’,这个是时候使用format 就很方便。

最新回答 / 慕村0394702
哪儿错了,说出来啊,至少贴个图。😘
全部 我要发布
最热 最新
只看我的

本次提问将花费2个积分

你的积分不足,无法发表

为什么扣积分?

本次提问将花费2个积分

继续发表请点击 "确定"

为什么扣积分?

账号登录 验证码登录

遇到问题
忘记密码

代码语言