|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
格式化字符串的format函数介绍,在python中,用此函数便可轻松格式化任意字符串。
- #(0)
- print('(0)')
- age = 27
- name = 'bob'
- print('{0} is a {1} years old man , and now he is strating stduy python.'.format(name,age))
- print('he is {} years old'.format(age))
- #(1)
- print('(1)')
- # 对于浮点数 '0.333' 保留小数点(.)后三位
- print('{0:.3f}'.format(1.0/3))
- # 使用下划线填充文本,并保持文字处于中间位置
- # 使用 (^) 定义 '___hello___'字符串长度为 11
- print('{0:_^11}'.format('hello'))
- # 基于关键词输出 'Swaroop wrote A Byte of Python'
- print('{name} wrote {book}'.format(name='Swaroop', book='A Byte of Python'))
复制代码
输出结果为
- (0)
- bob is a 27 years old man , and now he is strating stduy python.
- he is 27 years old
- (1)
- 0.333
- ___hello___
- Swaroop wrote A Byte of Python
复制代码
|
评分
-
查看全部评分
|