Python字符串的格式化的详细介绍

python教程评论1.3K views阅读模式

本篇文章给大家带来的内容是关于Python字符串的格式化的详细介绍,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

相信很多人在格式化字符串的时候都用"%s" % v的语法,PEP 3101 提出一种更先进的格式化方法 str.format() 并成为 Python 3 的标准用来替换旧的 %s 格式化语法,CPython 从 2.6 开始已经实现了这一方法(其它解释器未考证)。

format()

新的format() 方法其实更像是一个简略版的模板引起(Template Engine),功能非常丰富。

模板中替换变量用 {} 包围,且由 : 分为两部分,其中后半部分 format_spec 在后面会单独讨论。

前半部分有三种用法:

  • 代表位置的数字
  • 代表keyword的标识符

这与函数调用的参数类别是一致的

print("{} {}".format("Hello", "World"))
# 等同于以下几种
print("{0} {1}".format("Hello", "World"))
print("{hello} {world}".format(hello="Hello", world="World"))
print("{0}{1}{0}".format("H", "e"))

# Hello World
# Hello World
# Hello World
# HeH

除此之外,就像函数参数的解包一样,format() 中也可以直接使用解包操作

print("{author}.{city}".format(**{"author": "Miracle", "city": "上海"}))
print("{} {}".format(*["Miracle", "上海"]))

Miracle.上海
Miracle 上海

在模板中还可以通过 .identifier 和 [key] 的方式获取变量内的属性或值(需要注意的是 "{}{}" 相当于 "{0}{1}")

data = {'author': 'Miracle', 'like': 'papapa'}
print("Author: {0[author]}, Like: {0[like]}".format(data))
langs = ["Python", "Ruby"]
print("{0[0]} vs {0[1]}".format(langs))

print("\n====\nHelp(format):{.__doc__}".format(str.format))

# Name: Python, Score: 100
# Python vs Ruby

# ====
# Help(format):
#  S.format(*args, **kwargs) -> str

强制转换,可以通过 ! + r|s|a 的方式对替换的变量进行强制转换

  • "{!r}" 对变量调用 repr()
  • "{!s}" 对变量调用 str()
  • "{!a}" 对变量调用 ascii()

冒号之后的部分定义输出的样式

align 代表对齐方向,通常要配合 width 使用,而 fill 则是填充的字符(默认为空白):

for align, text in zip("<^>", ["left", "center", "right"]):
   # 务必看懂这句话
   print("{:{fill}{align}16}".format(text, fill=align, align=align))

print("{:0=10}".format(100)) # = 只允许数字

# left<<<<<<<<<<<<
# ^^^^^center^^^^^
# >>>>>>>>>>>right
# 0000000100

同时可以看出,样式设置里面可以嵌套 {} ,但是必须通过 keyword 指定,且只能嵌套一层。

接下来是符号样式:+|-|' ' 分别指定数字是否需要强制符号(其中空格是指在正数的时候不显示 + 但保留一位空格)

print("{0:+}\n{1:-}\n{0: }".format(3.14, -3.14))

# +3.14
# -3.14
# 3.14

用于表示特殊格式的数字(二进制、十六进制等)是否需要前缀符号

逗号也是用于表示数字时是否需要在千位处进行分隔

0 相当于前面的{:0=} 右对齐并用 0 补充空位

print("Binary: {0:b} => {0:#b}".format(3))
print("Large Number: {0:} => {0:,}".format(1.25e6))
print("Padding: {0:16} => {0:016}".format(3))

# Binary: 11 => 0b11
# Large Number: 1250000.0 => 1,250,000.0
# Padding:                3 => 0000000000000003

最后小胖给大家介绍一下熟悉的小数点精度问题, .n 和格式化类型。

这里仅给出一些示例,详细内容可以查阅文档:

from math import pi
print("pi = {pi:.2}, also = {pi:.7}".format(pi=pi))

# pi = 3.1, also = 3.141593

Integer

for t in "b c d #o #x #X n".split():
   print("Type {0:>2} of {1} shows: {1:{t}}".format(t, 97, t=t))

# Type  b of 97 shows: 1100001
# Type  c of 97 shows: a
# Type  d of 97 shows: 97
# Type #o of 97 shows: 0o141
# Type #x of 97 shows: 0x61
# Type #X of 97 shows: 0X61
# Type  n of 97 shows: 97

Float

for t, n in zip("eEfFgGn%", [12345, 12345, 1.3, 1.3, 1, 2, 3.14, 0.985]):
   print("Type {} shows: {:.2{t}}".format(t, n, t=t))

# Type e shows: 1.23e+04
# Type E shows: 1.23E+04
# Type f shows: 1.30
# Type F shows: 1.30
# Type g shows: 1
# Type G shows: 2
# Type n shows: 3.1
# Type % shows: 98.50%

String (default)

try:
   print("{:s}".format(123))
except:
   print("{}".format(456))

# 456

本篇文章到这里就已经全部结束了,更多其他精彩内容可以关注PHP中文网的python视频教程栏目!

以上就是Python字符串的格式化的详细介绍的详细内容,更多请关注php教程其它相关文章!

企鹅博客
  • 本文由 发表于 2019年7月22日 06:59:26
  • 转载请务必保留本文链接:https://www.qieseo.com/348276.html

发表评论