Python中字符串的Format用法。
2017-06-20 23:24
531 查看
一、例子:
二、结果:
"_".join(["1","2","3","4"]) "_".join(map(lambda x:str(x),[1,2,3,4])) "{0}-{1}".format(3.4,34) "{}-{}-{}".format(3,4,5) "{name}-{age}".format(**{'name':'song','age':34}) "{name}-{age}".format(name='song',age=34) "the list first is: {obj[0]}".format(obj=[1,2,3,4]) "the list first is: {0[0]},{0[1]}".format([1,2,3,4]) "{:>8}".format('abc') "{0:<8}".format('abc') "{0:_<8}".format('abc') "{0:0>8}".format('3.14') "{0:.2f}".format(123.123456) "{0:b}".format(1023) "{0:d}".format(0b1111111111) "{0:x}".format(1023) "{0:,}".format(102345.6789)
二、结果:
>>> "_".join(["1","2","3","4"]) '1_2_3_4' >>> "_".join(map(lambda x:str(x),[1,2,3,4])) '1_2_3_4' >>> "{0}-{1}".format(3.4,34) '3.4-34' >>> "{}-{}-{}".format(3,4,5) '3-4-5' >>> "{name}-{age}".format(**{'name':'song','age':34}) 'song-34' >>> "{name}-{age}".format(name='song',age=34) 'song-34' >>> "the list first is: {obj[0]}".format(obj=[1,2,3,4]) 'the list first is: 1' >>> "the list first is: {0[0]},{0[1]}".format([1,2,3,4]) 'the list first is: 1,2' >>> "{:>8}".format('abc') ' abc' >>> "{0:<8}".format('abc') 'abc ' >>> "{0:_<8}".format('abc') 'abc_____' >>> "{0:0>8}".format('3.14') '00003.14' >>> "{0:.2f}".format(123.123456) '123.12' >>> "{0:b}".format(1023) '1111111111' >>> "{0:d}".format(0b1111111111) '1023' >>> "{0:x}".format(1023) '3ff' >>> "{0:,}".format(102345.6789) '102,345.6789' >>>
相关文章推荐
- Python--format 函数格式化字符串的用法
- python格式化字符串format的用法
- python,格式化字符串函数.format()的用法
- python string.format的用法总结
- python 字符串模板用法string.Template
- DataFormatString属性用法(数据格式字符串)
- python学习之 字符串前'r'的用法
- python学习之 字符串前'r'的用法
- Python开发——字符串format
- Python 原始字符串(raw strings)的用法
- Python 在字符串前面加'r'的用法
- MFC字符串操作(二)CString.Format的用法
- Python Cookbook 第二版 汉化版 [Recipe 1.9] 简化字符串 translate 方法的用法
- Python .format(),%格式化时,对字符串编码方式的影响
- python format格式化字符串
- Delphi 使用 Format格式话字符串的用法
- Python 字符串格式化输出(format/printf)
- String.Format()返回字符串中有{}的用法
- python 字符串split的用法分享
- Python - 格式化(format())输出字符串 详解 及 代码