2019-02-12
阅读量:
709
python如何使用format函数格式化输出?
使用format函数格式化输出:
在Python(2.6)中添加了format()方法,而字符串的格式化方法需要更多手动操作。python可以用{}
来标记变量将被替换的位置,并且可以提供详细的格式化指令,但用户还需要提供要格式化的信息。这个函数的作用是可以通过位置格式连接输出中的元素。例如 :
代码:
# Python program showing
# use of format() method
# using format() method
print('I love {} for "{}!"'.format('Geeks', 'Geeks'))
# using format() method and refering
# a position of the object
print('{0} and {1}'.format('Geeks', 'Portal'))
print('{1} and {0}'.format('Geeks', 'Portal'))






评论(0)


暂无数据
推荐帖子
0条评论
0条评论
0条评论