云学编程的第5天—【微软官方python入门教程 P11-P12笔记】2021-11-05 格式化字符串

本文探讨了多种字符串格式化方法,包括使用加号连接、format函数、f-string以及百分比符号方法。作者特别提到了f-string的易读性和自文档化特性,并指出这种格式化方式在其他编程语言中也有类似实现。

P11 Custom string formatting

format strings——there are multiple ways that you can do the exact same types of string concatenation.

P12实操

first_name='liu'
last_name='ding yun'

output='hello ' + first_name + ' ' + last_name
output='hello,{}{}'.format(first_name,last_name)
output='hello,{0}{1}'.format(first_name,last_name)
output=f'hello,{first_name} {last_name}'
output='hello,%s %s'%(first_name,last_name)

print(output)

i really like that last string format with the little f at the very beginning there.⬇️ you'll also notice that there's a lot of other programming languages that have a very similar construct. I also really like the fact that it is self-documenting, because i can very clearly see......

first_name='liu'
last_name='ding yun'

# output='hello ' + first_name + ' ' + last_name
# output='hello,{}{}'.format(first_name,last_name)
# output='hello,{0}{1}'.format(first_name,last_name)
output=f'hello,{first_name} {last_name}'
# output='hello,%s %s'%(first_name,last_name)

print(output)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值