func_1 = ‘My hobby is %s‘ %‘play‘ #将play加到is后面
print(‘func_1:‘,func_1)
func_2 = ‘%s is a book %s ‘ %(‘This‘,‘.‘) #将This加到最前面,句号加到最后面
print(‘func_2:‘,func_2)
func_3 = ‘%s are %s doing‘ %([‘what‘],(‘you‘))
print(‘func_3:‘,func_3)
func_4 = ‘%s%sis a book%s‘ %(‘this‘,‘ ‘,‘.‘)
print(‘func_4:‘,func_4)
func_5 = ‘%.2s‘ %‘abcdef‘ #截取两个字符 a,b
print(‘func_5:‘,func_5) #输出a,b
st = ‘ABCDEFG‘
func_6 = ‘%.4s‘ %st #截取四个字符,abcd
print(‘func_6:‘,func_6 )
func_7 = ‘%11s‘ %st #左边补四个空格,然后在后面打印字符串
print(‘func_7:‘,func_7 )
func_8 = ‘%-11s%s‘ %(st,‘abc‘) #在字符串右面打印四个空格,然后在打印abc
print(‘func_8:‘,func_8 )
ma_1 = ‘1 + 1 = %d‘ %2
print(‘ma_1:‘,ma_1 )
ma_2 = ‘%.2f‘ %2.3472 #有四舍五入功能,.2f指保留两位小数,输出2.35
print(‘ma_2:‘,ma_2 )
ma_3 = ‘%.3f‘%2.3472 #.3f指保留三位小数
print(‘ma_3:‘,ma_3)
ma_4 = ‘%.2f %%‘%2.3472
print(‘ma_4:‘,ma_4 )
x = ‘i am %(name)s age %(age)d‘ % {"name":‘alex‘,‘age‘:18}
print(x)
print(‘root‘,‘4‘,‘4‘,sep="??
print(‘你好‘,end=‘‘) #end=‘‘表示不换行,在你好后面直接打印小王
print(‘小王‘)
原文:https://www.cnblogs.com/shadowfolk/p/14322661.html