Python中字符串格式化str.format
Python中字符串格式化str.format的详细介绍
转载 2017-02-17 作者:旷世的忧伤
我要评论
前言
Python 在 2.6 版本中新加了一个字符串格式化方法: str.format() 。它的基本语法是通过 {} 和 : 来代替以前的 %.。
格式化时的占位符语法:
|
1
|
replacement_field ::= "{" [field_name] ["!" conversion] [":" format_spec] "}" |
“映射”规则
通过位置
str.format() 可以接受不限个参数,位置可以不按顺序:
|
1
2
3
4
5
6
|
>>> "{0} {1}".format("hello", "world")'hello world'>>> "{} {}".format("hello", "world")'hello world'>>> "{1} {0} {1}".format("hello", "world")'world hello world' |
通过关键字参数
使用关键参数时字符串中需要提供参数名:
|
1
2
3
4
5
|
>>> "I am {name}, age is {age}".format(name="huoty", age=18)'I am huoty, age is 18'>>> user = {"name": "huoty", "age": 18}>>> "I am {name}, age is {age}".format(**user)'I am huoty, age is 18' |
通过对象属性
str.format() 可以直接读取用户属性:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
>>> class User(object):... def __init__(self, name, age):... self.name = name... self.age = age... ... def __str__(self):... return "{self.name}({self.age})".format(self=self)... ... def __repr__(self):... return self.__str__()... ...>>> user = User("huoty", 18)>>> userhuoty(18)>>> "I am {user.name}, age is {user.age}".format(user=user)'I am huoty, age is 18' |
通过下标
在需要格式化的字符串内部可以通过下标来访问元素:
|
1
2
3
4
5
|
>>> names, ages = ["huoty", "esenich", "anan"], [18, 16, 8]>>> "I am {0[0]}, age is {1[2]}".format(names, ages)'I am huoty, age is 8'>>> users = {"names": ["huoty", "esenich", "anan"], "ages": [18, 16, 8]}>>> "I am {names[0]}, age is {ages[0]}".format(**users) |
指定转化
可以指定字符串的转化类型:
|
1
|
conversion ::= "r" | "s" | "a" |
其中 "!r" 对应 repr(); "!s" 对应 str(); "!a" 对应 ascii()。 示例:
|
1
2
|
>>> "repr() shows quotes: {!r}; str() doesn't: {!s}".format('test1', 'test2')"repr() shows quotes: 'test1'; str() doesn't: test2" |
格式限定符
填充与对齐
填充常跟对齐一起使用。^, <, > 分别是居中、左对齐、右对齐,后面带宽度, : 号后面带填充的字符,只能是一个字符,不指定则默认是用空格填充。
|
1
2
3
4
5
6
7
8
9
10
11
12
|
>>> "{:>8}".format("181716")' 181716'>>> "{:0>8}".format("181716")'00181716'>>> "{:->8}".format("181716")'--181716'>>> "{:-<8}".format("181716")'181716--'>>> "{:-^8}".format("181716")'-181716-'>>> "{:-<25}>".format("Here ")'Here -------------------->' |
浮点精度
用 f 表示浮点类型,并可以在其前边加上精度控制:
|
1
2
3
4
5
6
7
8
|
>>> "[ {:.2f} ]".format(321.33345)'[ 321.33 ]'>>> "[ {:.1f} ]".format(321.33345)'[ 321.3 ]'>>> "[ {:.4f} ]".format(321.33345)'[ 321.3335 ]'>>> "[ {:.4f} ]".format(321)'[ 321.0000 ]' |
还可以为浮点数指定符号,+ 表示在正数前显示 +,负数前显示 -; (空格)表示在正数前加空格,在幅负数前加 -;- 与什么都不加({:f})时一致:
|
1
2
3
4
5
6
7
8
9
10
|
>>> '{:+f}; {:+f}'.format(3.141592657, -3.141592657)'+3.141593; -3.141593'>>> '{: f}; {: f}'.format(3.141592657, -3.141592657)' 3.141593; -3.141593'>>> '{:f}; {:f}'.format(3.141592657, -3.141592657)'3.141593; -3.141593'>>> '{:-f}; {:-f}'.format(3.141592657, -3.141592657)'3.141593; -3.141593'>>> '{:+.4f}; {:+.4f}'.format(3.141592657, -3.141592657)'+3.1416; -3.1416' |
指定进制
|
1
2
3
4
|
>>> "int: {0:d}; hex: {0:x}; oct: {0:o}; bin: {0:b}".format(18)'int: 18; hex: 12; oct: 22; bin: 10010'>>> "int: {0:d}; hex: {0:#x}; oct: {0:#o}; bin: {0:#b}".format(18)'int: 18; hex: 0x12; oct: 0o22; bin: 0b10010' |
千位分隔符
可以使用 "," 来作为千位分隔符:
|
1
2
|
>>> '{:,}'.format(1234567890)'1,234,567,890' |
百分数显示
|
1
2
|
>>> "progress: {:.2%}".format(19.88/22)'progress: 90.36%' |
事实上,format 还支持更多的类型符号:
|
1
|
type ::= "b" | "c" | "d" | "e" | "E" | "f" | "F" | "g" | "G" | "n" | "o" | "s" | "x" | "X" | "%" |
其他技巧
占位符嵌套
某些时候占位符嵌套还是很有用的:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
>>> '{0:{fill}{align}16}'.format("hello", fill='*', align='^')'*****hello******'>>>>>> for num in range(5,12):... for base in "dXob":... print("{0:{width}{base}}".format(num, base=base, width=5), end=' ')... print()... ... 5 5 5 101 6 6 6 110 7 7 7 111 8 8 10 1000 9 9 11 1001 10 A 12 1010 11 B 13 1011 |
作为函数使用
可以先不指定格式化参数,而是在不要的地方作为函数来调用:
|
1
2
3
|
>>> email_f = "Your email address was {email}".format>>> print(email_f(email="suodhuoty@gmail.com"))Your email address was sudohuoty@gmail.com |
转义大括号
当在字符串中需要使用大括号时可以用大括号转义:
|
1
2
|
>>> " The {} set is often represented as { {0} } ".format("empty")' The empty set is often represented as {0} ' |
# msg='i am %s my hobby is %s' % ('lhf','alex')
# print(msg)
#
# msg='i am %s my hobby is %s' % ('lhf',1)
# msg='i am %s my hobby is %s' % ('lhf',[1,2])
# print(msg)
# name='lhf'
# age=19
# msg='i am %s my hobby is %s' % (name,age)
# print(msg)
#打印浮点数
tpl = "percent %.2f" % 99.976234444444444444
print(tpl)
#打印百分比
tpl = 'percent %.2f %%' % 99.976234444444444444
print(tpl)
字典放进去
tpl = "i am %(name)s age %(age)d" % {"name": "alex", "age": 18}
print(tpl)
添加60个空格
msg='i am %(name)+60s my hobby is alex' %{'name':'lhf'}
print(msg)
添加颜色
msg='i am \033[43;1m%(name)+60s\033[0m my hobby is alex' %{'name':'lhf'}
print(msg)
print('root','x','0','0',sep=':')
# print('root'+':'+'x'+':'+'0','0')
# tpl = "i am {name}, age {age}, really {name}".format(name="seven", age=18)
如果里面是字典,加两个*号
# tpl = "i am {name}, age {age}, really {name}".format(**{"name": "seven", "age": 18})
如何里面是字典,加一个*号
# tpl = "i am {:s}, age {:d}".format(*["seven", 18])
# tpl = "i am {:s}, age {:d}".format("seven", 18) #["seven", 18]
#
# l=["seven", 18]
# tpl = "i am {:s}, age {:d}".format('seven',18)
# print(tpl)
tpl = "numbers: {:b},{:o},{:d},{:x},{:X}, {:%},{}".format(15, 15, 15, 15, 15, 15.87623, 2)
print(tpl)

浙公网安备 33010602011771号