0、今日筆記
在Python27中以及之后版本,新增了一種格式化字符串的函數,通過使用大括號 {} 和冒號 : 來實現以前的百分號 % 的寫法。
1、默認按順序填充,每個 {} 填充一個變量
hello = 'hello' world = 'world' hello_world = '{} {}'.format(hello, world) # 默認按順序填充,每個 {} 填充一個變量 print(hello_world) # hello world
2、指定填充順序, 0 表示第一個變量, 1 表示第二個變量, 以此類推
hello = 'hello' world = 'world' # 指定填充順序, 0 表示第一個變量, 1 表示第二個變量, 以此類推 hello_world = '{0} {1}'.format(hello, world) print(hello_world) # 支持多個順序寫法 hello_world_hello = '{0} {1} {0}'.format(hello, world) print(hello_world_hello)
3、指定 key 的填充方式
hello = 'hello' world = 'world' # 指定 key 的填充方式 hello_world = '{hello} {world}'.format(hello=hello, world=world) print(hello_world) # 指定 key 的填充方式, key 是隨意的 hello_world = '{hello_string} {world_string}'.format(hello_string=hello, world_string=world) print(hello_world)
4、保留N位小數
number = 10.123 # 保留兩位小數 number_string = '{:.2f}'.format(number) print(number_string) # 保留三位小數 number_string = '{:.3f}'.format(number) print(number_string) # 保留四位小數 number_string = '{:.4f}'.format(number) print(number_string) number = -10.123 # 保留兩位小數 number_string = '{:.2f}'.format(number) print(number_string) # 保留三位小數 number_string = '{:.3f}'.format(number) print(number_string) # 保留四位小數 number_string = '{:.4f}'.format(number) print(number_string)
5、居中對齊,湊足N位,不足用指定字符填充
hello = 'hello' # 居中對齊,湊足8位,不足用空格填充 hello = '{:^8}'.format(hello) print(hello) hello = 'hello' # 居中對齊,湊足8位,不足用字母x填充 hello = '{:x^8}'.format(hello) print(hello) hello = 'hello' # 居中對齊,湊足9位,不足用數字0填充 hello = '{:0^9}'.format(hello) print(hello)
6、左對齊,湊足N位,不足用指定字符填充
hello = 'hello' # 左對齊,湊足8位,不足用空格填充 hello = '{:<8}'.format(hello) print(hello) hello = 'hello' # 左對齊,湊足8位,不足用字母x填充 hello = '{:x<8}'.format(hello) print(hello) hello = 'hello' # 左對齊,湊足8位,不足用數字0填充 hello = '{:0<8}'.format(hello) print(hello)
7、右對齊,湊足N位,不足用指定字符填充
hello = 'hello' # 右對齊,湊足8位,不足用空格填充 hello = '{:>8}'.format(hello) print(hello) hello = 'hello' # 右對齊,湊足8位,不足用字母x填充 hello = '{:x>8}'.format(hello) print(hello) hello = 'hello' # 右對齊,湊足8位,不足用數字0填充 hello = '{:0>8}'.format(hello) print(hello)
8、百分比顯示,保留N位小數
number = 0.291 # 百分比顯示, 默認保存6位小數 number = '{:%}'.format(number) print(number) number = 0.291 # 百分比顯示, 保存2位小數 number = '{:.2%}'.format(number) print(number) number = 0.291 # 百分比顯示, 保存3位小數 number = '{:.3%}'.format(number) print(number)
9、指數顯示,保留N位小數
number = 100000 # 指數形式顯示, 默認保存6位小數 number = '{:e}'.format(number) print(number) number = 100000 # 指數形式顯示, 保存2位小數 number = '{:.2e}'.format(number) print(number) number = 100000 # 指數形式顯示, 保存3位小數 number = '{:.3e}'.format(number) print(number)
10、進制顯示
number = 10 # 二進制顯示 number_string = '{:b}'.format(number) print(number_string) # 十進制顯示 number_string = '{:d}'.format(number) print(number_string) # 八進制顯示 number_string = '{:o}'.format(number) print(number_string) # 十六進制小字母顯示 number_string = '{:x}'.format(number) print(number_string) # 十六進制大字母顯示 number_string = '{:X}'.format(number) print(number_string)
11、大括號轉義大括號,原樣顯示大括號
hello = 'hello' # 用大括號可以轉義大括號 hello = '{},{{我只是個單純的大括號}}'.format(hello) print(hello)
12、字符串前面加入字母 f 也能進行格式化字符串
hello = 'hello' world = 'world' # 字符串前面加入字母 f 也能進行格式化字符串 hello_world = f'{hello} {world}' print(hello_world)