Python字符串占位符

Python字符串占位符

%s

asd='hello %s'%'世界'

如果有多处占位符

aaa='hello %s 你好 %s'%('世界','中国')

限制占位符字数最小是三位数最大8位数,当最小值不足时会使用空格补全

b='hello %3.8s'%'世界'

注:%s中的s表示String(字符串)

 %f

保留小数点后一位,自带四舍五入的功能

c='hello %.1f'%154.26

注:%f中的f表是float(浮点类型)

 %d

整数类型,直接舍去小数点后面数字

d='hello %d'%154.26

注:%d中的d是int(整数类型)

格式化字符串

f = '你好'
h = '世界'
n = f'{f}{h}'
print(n)
print(f'{f}')

{}中的值可以是任意有效变量 

 

Tags: