py-day2-5 python 百分号字符串拼接

时间:2021-11-12 11:15:50
#### 字符串格式化。
# %s 代替任何的元素 (数字,字符串,列表··)
print('I live %s crty' %'my')
print('I live %s crty' %'[6,8,9]')
I live my crty
I live [6,8,9] crty # %s -- %( ) 可以代替多个元素
print('I live %s crty,prefer live %s country' %('your','my')) I live your crty,prefer live my country # %d 表示代替数字 也只能接收数字
msg = 'i an %s my age is %d'%('xiaoma',18)
print(msg) i an xiaoma my age is 18 # 打印浮点数 默认保留小数点后6位数 并四舍五入
msg = 'percent %f'%3.1415926
print(msg) percent 3.141593 msg1 = 'percent %.2f'%3.1415926 #(%.2f 保留小数点后2位数 )
print(msg1) percent 3.14 # 百分比
msg2 = 'percent %f %%'%3.1415926
print(msg2) percent 3.141593 % msg3 = 'percent %.2f %%'%3.1415926
print(msg3) percent 3.14 %
# 以字典的形式上传
msg = 'i am %(name)s my age is %(age)d' % {'name':'xiaoma','age':18}
print(msg) i am xiaoma my age is 18
#  + -  是调节左右宽度    \033[色号;1m --- \033[0m    (表示填充颜色)
test = 'i am %-20s my hobby is %s' %('mj',18)
print(test)
test1 = 'i am %+20s my hobby is %s' %('mj',18)
print(test1) i am mj my hobby is 18
i am mj my hobby is 18 msg = 'i am \033[43;1m%(name)+20s\033[0m my hobby is alex' %{'name':'majun'}
print(msg)

py-day2-5 python 百分号字符串拼接

# 符号拼接
print('rood','x','uid','eid',sep= ':')
print('rood','x','uid','eid',sep= "-") rood:x:uid:eid
rood-x-uid-eid