python基础之格式化字符串
格式化字符串
a
=3
b
=5
print(str(a
)+'+'+str(b
)+'='+str(a
+b
))
print('%s+%s=%s'%(a
,b
,a
+b
))
info
='我叫%s,你叫%s,他叫%s,今年是%d年'%('桃子猫的布偶','二狗','隔壁老王',2020)
print(info
)
info
='我叫%5s,你叫%5s,他叫%5s,今年是d年'%('桃子猫的布偶','二狗','隔壁老王',20)
print(info
)
info
='我叫%-5s,你叫%-5s,他叫%-5s,今年是%-05d年'%('桃子猫的布偶','二狗','隔壁老王',20)
print(info
)
number1
='%6.1f'%(3.68)
print(number1
)
str1
='My name is{:6},Your name is{:6},age is{:6}'.format('桃子猫的布偶','二狗',23)
print(str1
)
str1
='My name is{:>6},Your name is{:^9},age is{:<6}'.format('桃子猫的布偶','二狗',23)
print(str1
)
str1
='My name is{:>6},Your name is{:^9},age is{:<6}'.format('桃子猫的布偶','二狗',23,24)
print(str1
)
str1
='My name is{:>6},Your name is{:^9},age is{:06}'.format('桃子猫的布偶','二狗',23)
print(str1
)
str1
='My name is{0:>6},Your name is{0:^9},age is{0:6}'.format('桃子猫的布偶','二狗',23)
print(str1
)
str1
='My name is{0},Your name is{1},age is{2}'.format('桃子猫的布偶','二狗',23)
print(str1
)
str1
='My name is{0},Your name is{{{1}}},age is{2}'.format('桃子猫的布偶','二狗',23)
print(str1
)
name1
='桃子猫的布偶'
name2
='二狗'
print(f
'My name is{name1},Your name is{name2}')
打印结果演示
3+5=8
3+5=8
我叫桃子猫的布偶
,你叫二狗
,他叫隔壁老王
,今年是
2020年
我叫桃子猫的布偶
,你叫 二狗
,他叫 隔壁老王
,今年是
00020年
我叫桃子猫的布偶
,你叫二狗
,他叫隔壁老王
,今年是
20 年
3.7
My name
is桃子猫的布偶
,Your name
is二狗
,age
is 23
My name
is桃子猫的布偶
,Your name
is 二狗
,age is23
My name
is桃子猫的布偶
,Your name
is 二狗
,age is23
My name
is桃子猫的布偶
,Your name
is 二狗
,age is000023
My name
is桃子猫的布偶
,Your name
is 桃子猫的布偶
,age
is桃子猫的布偶
My name
is桃子猫的布偶
,Your name
is二狗
,age is23
My name
is桃子猫的布偶
,Your name
is{二狗
},age is23
My name
is桃子猫的布偶
,Your name
is二狗
转载请注明原文地址:https://blackberry.8miu.com/read-311.html