Python中字符串的Format用法。
一、例子:
"_".join(["","","",""])
"_".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'
>>> "_".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)
''
>>> "{0:d}".format(0b1111111111)
''
>>> "{0:x}".format(1023)
'3ff'
>>> "{0:,}".format(102345.6789)
'102,345.6789'
>>>
Python中字符串的Format用法。的更多相关文章
- python中字符串的一些用法
		一.字符串的拼接: a=‘123’ b=‘abc’ d=‘hello world’ 1.print(a+b) 2.print(a,b) 3. c=‘ ’.join((a ... 
- Python中字符串String的基本内置函数与过滤字符模块函数的基本用法
		Python中字符串String的基本内置函数与用法 首先我们要明白在python中当字符编码为:UTF-8时,中文在字符串中的占位为3个字节,其余字符为一个字节 下面就直接介绍几种python中字符 ... 
- 超详细!盘点Python中字符串的常用操作
		在Python中字符串的表达方式有四种 一对单引号 一对双引号 一对三个单引号 一对三个双引号 a = 'abc' b= "abc" c = '''abc''' d = " ... 
- Python中字符串有哪些常用操作?纯干货超详细
- python中字符串的操作方法
		python中字符串的操作方法大全 更新时间:2018年06月03日 10:08:51 作者:骏马金龙 我要评论这篇文章主要给大家介绍了关于python中字符串操作方法的相关资料,文中通过示例代码详细 ... 
- Python中sorted()方法的用法
		Python中sorted()方法的用法 2012-12-24 22:01:14| 分类: Python |字号 订阅 1.先说一下iterable,中文意思是迭代器. Python的帮助文档中对i ... 
- Python中int()函数的用法浅析
		int()是Python的一个内部函数 Python系统帮助里面是这么说的 >>> help(int) Help on class int in module __builti ... 
- Python中字符串的学习
		Python中字符串的学习 一.字符串的格式化输出 % 占位符 %s 字符串 %d integer %x 十六进制 integer %f float 指定长度 %5d 右对齐,不足左边补空格 %-5d ... 
- 【Python从入门到精通】(九)Python中字符串的各种骚操作你已经烂熟于心了么?
		您好,我是码农飞哥,感谢您阅读本文,欢迎一键三连哦. 本文将重点介绍Python字符串的各种常用方法,字符串是实际开发中经常用到的,所有熟练的掌握它的各种用法显得尤为重要. 干货满满,建议收藏,欢迎大 ... 
随机推荐
- iPhone开发过程中调试多次Release问题  message sent to deallocated
			初级:第一步 为程序添加符号断点 malloc_error_break 方法如下. 目标效果:让程序崩溃时跳转到出错到那一行.但是往往达不到这个效果.不行就继续往下看. At times, wh ... 
- layoutSubviews 在什么情况下会被触发
			layoutSubviews在以下情况下会被调用: 1.init初始化不会触发layoutSubviews 2.addSubview会触发layoutSubviews 3.设置view的Frame会触 ... 
- WCF:该不该用枚举值
			WCF支持枚举,不过在个别场景下会出现服务消费失败,如:传递或返回的枚举值(本质是int或其它)没有在枚举中定义.这种异常还很难定位,出现这种情况一般是因为BUG,因此简单的放弃使用枚举可能不是一个明 ... 
- Java ArrayList、string、string[]之间的转换
			1.ArrarList 转换为 string[] : ArrayList list = new ArrayList(); list.Add("aaa"); list.Add(&qu ... 
- localhost与127.0.0.1及本机ip的区别
			很多人会接触到这个ip地址127.0.0.1.也许你会问127.0.0.1是什么地址?其实127.0.0.1是一个回送地址,指本地机,一般用来测试使用.大家常用来ping 127.0.0.1来看本地i ... 
- UIWebView加载CSS样式的html
			UIWebView加载CSS样式的html 效果 源码 // // ViewController.m // CSS // // Created by YouXianMing on 16/7/19. / ... 
- Android之在string.xml配置文字颜色粗体等效果
			string.xml <string name="exchange_txt_hint"><Data><