30 - 字符串的format方法
【摘要】 1. 字符串的format方法有几种指定参数的方法
默认方式(传入的参数与{} 一一对应)命名参数位置参数{2}
2. 请详细描述字符串的format方法如何格式化字符串
s1 = 'Today is {}, the temperature is {} degress.'
print(s1.format('Saturday', 24))
123
Today i...
1. 字符串的format方法有几种指定参数的方法
- 默认方式(传入的参数与{} 一一对应)
- 命名参数
- 位置参数{2}
2. 请详细描述字符串的format方法如何格式化字符串
s1 = 'Today is {}, the temperature is {} degress.'
print(s1.format('Saturday', 24))
- 1
- 2
- 3
Today is Saturday, the temperature is 24 degress.
- 1
s2 = 'Today is {day}, the temperature is {degree} degress.'
print(s2.format(degree = 30, day = 'Sunday'))
- 1
- 2
Today is Sunday, the temperature is 30 degress.
- 1
s3 = 'Today is {day},{} the {} temperature is {degree}'
print(s3.format('abcd' , 12345, degree=24, day='Sunday'))
- 1
- 2
Today is Sunday,abcd the 12345 temperature is 24
- 1
s4 = 'Today is {day}, {1}, the {0} temperature is {degree} degrees.'
print(s4.format('abcd', 12345, degree=24, day='Sunday'))
- 1
- 2
Today is Sunday, 12345, the abcd temperature is 24 degrees.
- 1
class Person: def __init__(self): self.age = 20 self.name = 'Bill' person = Person()
s5 = 'My name is {p.name}, my age is {p.age}'
print(s5.format(p = person))
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
My name is Bill, my age is 20
- 1
文章来源: ruochen.blog.csdn.net,作者:若尘,版权归原作者所有,如需转载,请联系作者。
原文链接:ruochen.blog.csdn.net/article/details/104395810
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)