[Python]
阿里云国内75折 回扣 微信号:monov8 |
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6 |
场景
-
在开发网络应用的时候会用到
URL
的一些拼接替换处理。比如某个链接的部分需要替换为其他内容其他部分保持不变那么有没有快捷的字符串模板替换库呢 -
在阅读第三方代码时会看到
f"xxxx{1}"
这类的字符串这是什么语法
说明
-
最常用的是
print
风格的格式化字符串如print('%s,%s %s' % ("Hi,", "Hello", "World"))
。 这种格式化风格是最早的字符串格式化方式这种方式有它自己的特点:- 如参数列表必须一一对应
%
声明顺序。 - 有多少个
%
就必须有多少个值值不能重用。 比如str.format()
格式化可以支持值重用"{1},{0},{1}".format(1,2)
, 或者f"{name}.{value}.{name}"
- 不支持对象格式化比如
datetime
对象。 - 数值不支持格式化为二进制格式。
- 如参数列表必须一一对应
-
最新的字面常亮
f""
格式化就是str.format()
的简化版具备它大多数的格式语法。最常见的就是命名参数格式化。
name = "Tobey"
line = f"My name is {name} or {name!s} or {name!r} or {repr(name)}"
print(line)
f""
和str.format()
支持数值二进制输出.
number = 1024
line = f"{number:#0x}" # 0x400
print(line)
line = f"{number:b}" # 10000000000
- 输出时间格式等[3]。
today = datetime(year=2023, month=2, day=1)
line = f"{today:%Y%m%d}" # 20230201
print(line)
例子
- 这里有三种字符串格式方法, 详细的使用方式看例子.
import decimal
from datetime import datetime
def TestPrecentSignFormat():
print("==== TestPrecentSignFormat BEGIN ====")
line = '%s,%s %s' % ("Hi,", "Hello", "World")
print(line)
line = "%s" % "https://blog.csdn.net/infoworld"
print(line)
number = 1037
line = "hex is %x or %X" % (number,number)
print(line)
line = "Decimal is %d" % number
print(line)
line = "Float is %03.2f" % number
print(line)
print("==== TestPrecentSignFormat END ====")
def TestStringFormat():
print("==== TestStringFormat BEGIN ====")
# 使用{n}来表示第n个参数,n是数值
line = "{1},{2},{0}".format(0,1,2) # 1,2,0
print(line)
# 如果不使用序号那么代表着顺序
line = "{},{},{}".format(0,1,2) # 0,1,2
print(line)
# *符号表示解包参数序列
line = "{1},{2},{0}".format(*[11,22,33])
print(line)
# 所有f""可以使用的语法都可以使用, 对象格式化需要加{:xxx}格式
today = datetime(year=2023, month=2, day=1)
line = "{:%Y%m%d}".format(today) # 20230201
print(line)
# 数值格式化
number = 1023.2023
line = "{:03.2f}".format(number)
print(line)
# 自定义参数名
line = 'Coordinates: {latitude}, {longitude}'.format(latitude='37.24N', longitude='-115.81W')
print(line)
print("==== TestStringFormat END ====")
def TestFormatStringLiterals():
print("==== TestFormatStringLiterals BEGIN ====")
# 字符串字面常亮格式化使用str.format()方法来格式化。
# 作为模板变量
# 在3.6版本以后支持
name = "Tobey"
# !s相当于str(), !r相当于repr()
line = f"My name is {name} or {name!s} or {name!r} or {repr(name)}"
print(line)
# 格式化数据类型,使用:符号
precision = 5 # 精度是5位
value = decimal.Decimal("12.34567")
print(value)
line = f"result: {value:{precision}}"
print(line)
line = f"{value:03.3f}"
print(line)
# 格式化输出日期字符串
today = datetime(year=2023, month=2, day=1)
line = f"{today:%Y%m%d}" # 20230201
print(line)
# 数值数值的十六进制
number = 1024
line = f"{number:#0x}" # 0x400
print(line)
line = f"{number:b}" # 10000000000
print(line)
# object, 字符串里如果是引用属性不要使用"双引号
o1 = {"name": "product.id","value": 23}
line = f"Json name is {o1['name']}"
print(line)
# 输出大括号本身
line = f"{{ \" and }}"
print(line)
print("==== TestFormatStringLiterals END ====")
if __name__ == '__main__':
print("hello world!")
TestPrecentSignFormat()
TestStringFormat()
TestFormatStringLiterals()
输出
hello world!
==== TestPrecentSignFormat BEGIN ====
Hi,,Hello World
https://blog.csdn.net/infoworld
hex is 40d or 40D
Decimal is 1037
Float is 1037.00
==== TestPrecentSignFormat END ====
==== TestStringFormat BEGIN ====
1,2,0
0,1,2
22,33,11
20230201
1023.20
Coordinates: 37.24N, -115.81W
==== TestStringFormat END ====
==== TestFormatStringLiterals BEGIN ====
My name is Tobey or Tobey or 'Tobey' or 'Tobey'
12.34567
result: 12.34567
12.346
20230201
0x400
10000000000
Json name is product.id
{ " and }
==== TestFormatStringLiterals END ====