Python学习第三篇

五、输出输入格式化

1、% 方式

# % 形式
msg1 = 'I am %s,%d years old.I like %s !' % ('alex',20,'Python')
# dict传值
msg2 = 'I am %(name)s,%(age)d years old.I like %(course)s !' % {'name':'alex','age':20,'course':'Python'}
# 截取
tip = 'The price is %.2f $' % (99.9845)
print(msg1)
print(msg2)
print(tip)
I am alex,20 years old.I like Python !
I am alex,20 years old.I like Python !
The price is 99.98 $

Process finished with exit code 0

2、format 方式

# format常见形式
# 一般形式
msg1 = "I am {0},{1} years old.I like {2} !".format("alex", 20, "python")
msg2 = "I am {name},{age} years old.I like {course} !".format(name="alex", age=20, course="python")
# 字典形式
msg3 = "I am {name},{age} years old.I like {course} !".format(**{"name": "alex", "age": 20, "course": "python"})
# 列表形式
msg4 = "I am {:s},{:d} years old.I like {:s} !".format(*["alex", 20, "python"])
print(msg1, msg2, msg3, msg4)
I am alex,20 years old.I like python ! I am alex,20 years old.I like python ! I am alex,20 years old.I like python ! I am alex,20 years old.I like python !

Process finished with exit code 0

  https://www.cnblogs.com/huanglinqiang/articles/13272806.html

posted @ 2020-07-10 10:42  Dammond  阅读(64)  评论(0)    收藏  举报