python字符串


一、百分号%

# %s 占位符 格式化字符
print("格式化内容为 %s" % "hello")
str = "world"
print("格式化内容为 %s" % str)
# %[-][+][0][m][.n] 格式化字符 % 参数
template = "格式化内容为 %s"
print(template % "hello")

# m 占位宽度(默认右对齐)
template = "格式化内容为 |%20s|"
print(template % "hello")

# m 占位宽度(-左对齐)
template = "格式化内容为 |%-20s|"
print(template % "hello")

# m 占位宽度(+右对齐)
template = "格式化内容为 |%+20s|"
print(template % "hello")

# m 占位宽度(0对字符串没有意义)
template = "格式化内容为 |%020s|"
print(template % "hello")

# m 占位宽度(.n为截取前几个)
template = "格式化内容为 |%20.2s|"
print(template % "hello")
template = "格式化内容为 |%.2s|"
print(template % "hello")

# 多占位符对齐 必须使用元组
template = "格式化内容为 |%20s|%12s|"
print(template % ("hello", "2020-07-08"))
print(template % ("hello world", "2020-07-08"))

# %d 十进制 %x 十六进制 %o 八进制
template = "格式化内容为 |%d|"
print(template % 13)

template = "格式化内容为 |%x|"
print(template % 13)

template = "格式化内容为 |%o|"
print(template % 13)

template = "格式化内容为 |%10d|"
print(template % 13)

template = "格式化内容为 |%-10d|"
print(template % 13)

# 如果是%+形式 右侧对齐 正数会带+ 负数不变
template = "格式化内容为 |%+10d|"
print(template % 13)
print(template % -13)

# %0补全数字全0到占位位数
template = "格式化内容为 |%010d|"
print(template % 13)
print(template % -13)

# 浮点数 %f 默认六位小数
template = "格式化内容为 |%f|"
print(template % 1234.5678)

template = "格式化内容为 |%.4f|"  # 四位小数浮点数
print(template % 1234.5678)

template = "格式化内容为 |%.2f|"  # 四舍五入而不是截取 不足填0
print(template % 1234.5678)
print(template % 1234.5)

template = "格式化内容为 |%20.2f|"
print(template % 1234.5678)

template = "格式化内容为 |%-20.2f|"
print(template % 1234.5678)

template = "格式化内容为 |%+20.2f|"  # 右侧对齐 正数会带+ 负数不变
print(template % 1234.5678)

template = "格式化内容为 |%020.2f|"  # 前补0
print(template % 1234.5678)

# 多参数
par = ("ycz", 18, "男")
template = "姓名%s 年龄%d 性别%s"
print(template % par)

# 千分位 format
template = "千分位 %s"
print(template % format(12345.6789, ","))

二、format

template = "格式化内容为{}"
print(template.format("hello"))
# {[index][:[[fill]align][sign][#][m][,][.n][type]]}
# index 填充序号 键
# fill  填充字符
# align 对齐方式 <左对齐 >右对齐 ^居中对齐 =右对齐符号在最左侧 需要配合占位宽度使用
# sign  符号选项 + 正数正号 负数符号   - 正数不变 负数符号   空格 正数加空格 负数负号
# #井号  十六进制0x 八进制0o 二进制0b
# m     占位宽度
# ,     千分位
# .n    小数位数
# type  格式化字符类型 s d b x o f

# index
print("{} {} {}".format(1, 2, 3))
# print("{} {} {}".format(1, 2))  # 报错
print("{0} {1} {0}".format(1, 2))
print("这个数字是{0} 是{0} 是{0}".format(13))

print("{:s}".format("hello"))
print("|{:20s}|".format("hello"))  # 字符串默认左对齐
print("|{:20.2s}|".format("hello"))  # 字符串截取
print("|{:>20.2s}|".format("hello"))  # 字符串右对齐
print("|{:^20.2s}|".format("hello"))  # 字符串居中
print("|{:+^20.2s}|".format("hello"))  # 字符串居中 采用加号填充

# d x o b 进制
print("|{:20d}|".format(13))  # 十进制 数字默认右对齐
print("|{:0>20d}|".format(13))  # 十进制 数字默认右对齐 0补全
print("|{:020d}|".format(13))  # 十进制 数字默认右对齐 0补全的省略写法
# 数字的不同表示方式
print("|{0:d}|{0:x}|{0:o}|{0:b}|".format(13))
print("|{0:d}|{0:#x}|{0:#o}|{0:#b}|".format(13))  # 并显示进制表示方式

# + - 空格
print("|{:d}|{:d}|".format(13, -13))
print("|{:+d}|{:+d}|".format(13, -13))  # 正数加正号
print("|{:-d}|{:-d}|".format(13, -13))  # 不变
print("|{: d}|{: d}|".format(13, -13))  # 正数空格占位符号

# 浮点数f
print("{:f}".format(12345.6789))
print("{:.2f}".format(12345.6789))  # 保留两位小数
print("{:20.2f}".format(12345.6789))  # 保留两位小数 占位20

# 参数传递 index
print("关键字传参{name} {age:0>10d} {sex}".format(name="ycz", age=18, sex="man"))
par = (1, 2, 3)
print("{0[0]} {0[1]} {0[2]}".format(par))  # 传递元组
dic = {"x": 1, "y": 2, "z": 3}
print("{0[x]} {0[y]} {0[z]}".format(dic))  # 传递字典
print("{:,.2f}元".format(12345.6789))  # 千分位

三、f-string

name = "ycz"
age = 18
salary = 19992.11
print("姓名{}年龄{}薪水{}".format(name, age, salary))
print(f"姓名{name.capitalize():.2s}年龄{age:010d}薪水{salary:,.2f}")
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值