format是字符串内嵌的一个办法,用于格式化字符串。以大括号{}来表明被替换的字符串,肯定水平上与%目标统一。但在某些方面更加的不便

1、根本用法

1、依照{}的程序顺次匹配括号中的值

s = "{} is a {}".format('Tom', 'Boy')print(s) # Tom is a Boys1 = "{} is a {}".format('Tom')# 抛出异样, Replacement index 1 out of range for positional args tupleprint(s1)

2、通过索引的形式去匹配参数

这里须要留神的是,索引从0开始计算。

s = "{0} is a {1}".format('Tom', 'Boy')print(s) # Tom is a Boys1 = "{1} is a {2}".format('Tom', 'Lily', 'Girl')print(s1) # Lily is a Girl

字符串中索引的程序能够打乱,并不影响匹配。

s = "{1} is a {0}".format('Boy', 'Tom', )print(s) # Tom is a Boy

3、通过参数名来匹配参数

s = "{name} is a {sex}".format(name='Tom', sex='Boy')print(s) # Tom is a Boy

同理,如果参数曾经确定,能够间接利用{}进行格式化援用。

name = 'Tom'sex = 'Girl'# 以f结尾示意在字符串中反对大括号内的python表达式s = f"{name} is a {sex}"print(s) # Tom is a Boy

4、混搭应用

能够通过索引,参数名来混搭进行匹配。

s = "My name is {}, i am {age} year old, She name is {}".format('Liming', 'Lily', age=10)print(s) # My name is Liming, i am 10 year old, She name is Lily

须要留神的是,命名参数必须写道最初。负责会编译报错!

s = "My name is {}, i am {age} year old, She name is {}".format('Liming', age=10, 'Lily')print(s)  # SyntaxError: positional argument follows keyword argument

另外,不能够索引和默认格式化混合应用。

s = "{} is a {0}".format('Boy', 'Tom', )print(s)s1 = "{} is a {1}".format('Boy', 'Tom', )print(s1)

以上两种写法均报异样。

# ValueError: cannot switch from automatic field numbering to manual field specification

2、进阶用法

1、反对对参数局部援用

能够通过索引对参数的局部进行取值。如下:s[0] = w。

s = "The word is {s}, {s[0]} is initials".format(s='world')# The word is world, w is initialsprint(s)

2、数字的解决

一般的间接匹配数字没什么好说的,与根底局部的字符串匹配一样。

s = ' is {}'.format(3.1415926)print(s) #  is 3.1415926

如何应用format 保留两位小数呢? 须要应用:.2f,在用%进行格式化时咱们应用的是%:.2f

s = ' is {:.2f}'.format(3.1415926)print(s) #  is 3.14s1 = ' is %.2f'% 3.1415926print(s1) #  is 3.14

同时这种办法还能够用于字符串截取,不过数字前面就不能加f了。

s = "{:.1}".format('Hello')print(s) # H 

给数字加千位符

s = "{:,}".format(1000000)print(s) # 1,000,000

将数字转换成二进制

s = "{:b}".format(8)print(s) # 1000

将数字转换成八进制

s = "{:o}".format(8)print(s) # 10

将数字转换成十六进制

s = "{:X}".format(12)print(s) # C

总结如下

  • b: 输入整数的二进制形式;
  • c: 输入整数对应的 Unicode 字符;
  • d: 输入整数的十进制形式;
  • o: 输入整数的八进制形式;
  • x: 输入整数的小写十六进制形式;
  • X: 输入整数的大写十六进制形式;

3、格局解决

通过:+数字指定转换后的字符串长度,有余的局部用空格补充

s = "{:2}b".format('a')print(s) # a b  (a前面补了一个空格)# 如果指定的长度小于参数的长度,依照原参数匹配s1 = "{:2}World".format('Hello')print(s1) # HelloWorld

4、字符的填充

可通过:符号^数字进行字符串的填充。 其中数字为填充后的字符串总长度。

s = "{:*^10}".format('Hello')print(s) # **Hello***s = "{:-^20}".format('123456')print(s) # -------123456-------

如果数字小于字符串的长度,则不进行填充操作。

s = "{:*^3}".format('Hello')print(s) # Hello

5、list、tuple的拆分

在format格式化时,可应用* 或者 ** 进行对list、tuple拆分。

foods = ['fish', 'beef', 'fruit']s = 'i like eat {} and {} and {}'.format(*foods)# i like eat fish and beef and fruitprint(s)
foods = ['fish', 'beef', 'fruit']s = 'i like eat {2} and {0} and {1}'.format(*foods)# i like eat fruit and fish and beefprint(s)
dict_temp = {'name': 'Lily', 'age': 18} # 字典须要用 ** 进行拆分s = 'My name is {name}, i am {age} years old'.format(**dict_temp)print(s) # My name is Lily, i am 18 years old

史上最全Python材料汇总(长期更新)。隔壁小孩都馋哭了 --- 点击支付