其他分享
首页 > 其他分享> > 字符串格式化

字符串格式化

作者:互联网

字符串拼接

#字符串拼接
print("i am %s ,my hobby is eat"%'yoki')

msg = "i am %s ,my hobby is eat"%'yoki'
print(msg)

msg = "i am %s ,my hobby is %d"%('yoki',2)
print(msg)

# msg = "i am %s ,my hobby is %d"%('yoki',[1, 2])
# print(msg) #程序报错

msg = "i am %s ,my hobby is %s"% ('yoki',[1, 2])
print(msg)

##  %s可以接收一切,%d只能接收数字

name = "yoki"
age = 21
print("i an %s ,my age is %s" % (name, age))

##打印浮点数
num = "percent %f" % 99.46541656
print(num) #默认保留六位小数
num = "percent %.2f" % 99.46541656
print(num)

num = "percent %.4s" % 99.46541656
print(num) #99.4  ###截取字符串,不会四舍五入

## 打印百分比
num = "percent %.2f %%" % 99.46541656
print(num) #percent 99.47 %

msg = "i am %(name)s ,my age is %(age)d"%{"name":"yuqi","age":20}
print(msg)

# + 右对齐;正数前加正好,负数前加负号;
# -  左对齐;正数前无符号,负数前加负号;
# 空格 右对齐;正数前加空格,负数前加负号;
# 0  右对齐;正数前无符号,负数前加负号;用0填充空白处

msg = "i am %(name)-60s ,my age is %(age)d"%{"name": "yuqi", "age": 20}
print(msg)

msg = "i am \033[43;1m%(name)-60s\033[0m,my age is %(age)d"%{"name": "yuqi", "age": 20}
print(msg)

print('root', 'x', '0', '0', sep=":") # root:x:0:0

format字符格式化

#字符串拼接
print("i am %s ,my hobby is eat"%'yoki')

msg = "i am %s ,my hobby is eat"%'yoki'
print(msg)

msg = "i am %s ,my hobby is %d"%('yoki',2)
print(msg)

# msg = "i am %s ,my hobby is %d"%('yoki',[1, 2])
# print(msg) #程序报错

msg = "i am %s ,my hobby is %s"% ('yoki',[1, 2])
print(msg)

##  %s可以接收一切,%d只能接收数字

name = "yoki"
age = 21
print("i an %s ,my age is %s" % (name, age))

##打印浮点数
num = "percent %f" % 99.46541656
print(num) #默认保留六位小数
num = "percent %.2f" % 99.46541656
print(num)

num = "percent %.4s" % 99.46541656
print(num) #99.4  ###截取字符串,不会四舍五入

## 打印百分比
num = "percent %.2f %%" % 99.46541656
print(num) #percent 99.47 %

msg = "i am %(name)s ,my age is %(age)d"%{"name":"yuqi","age":20}
print(msg)

# + 右对齐;正数前加正好,负数前加负号;
# -  左对齐;正数前无符号,负数前加负号;
# 空格 右对齐;正数前加空格,负数前加负号;
# 0  右对齐;正数前无符号,负数前加负号;用0填充空白处

msg = "i am %(name)-60s ,my age is %(age)d"%{"name": "yuqi", "age": 20}
print(msg)

msg = "i am \033[43;1m%(name)-60s\033[0m,my age is %(age)d"%{"name": "yuqi", "age": 20}
print(msg)

print('root', 'x', '0', '0', sep=":") # root:x:0:0

########################################format字符串格式化###########
msg = "i am {0},age {1},sex {2}".format("yoki", 18, "女")
print(msg)

msg = "i am {1},age {1},sex {1}".format("yoki", 18, "女")
print(msg)

msg = "i am {name}, age {age}".format(name="yoki", age=18)
print(msg)

msg = "i am {name}, age {age}".format(**{"name": "yoki", "age": 18})
print(msg)

msg = "i am {0[0]}, age {1[1]}".format([1, 2, 3], [11, 22, 33])
print(msg)

msg = "i am {:s}, age {:d}, money {:f}".format("yoki", 18, 22.22)
print(msg)

msg = "i am {:s}, age {:d}, money {:f}".format(*["yoki", 18, 33.33]) #接收到的是列表
print(msg)

msg = "i am {:s}, age {:d}, money {:f}".format(*{"yoki", 18, 44.44}) #接收到的是字典
print(msg)

msg = "i am {name:s}, age {age:d}".format(name="yoki", age=18)
msg = "i am {name:s}, age {age:d}".format(**{"name": "yoki", "age": 18})


# b,将10进制整数自动转换成2进制表示然后格式化
# c,将10进制整数自动转换为其对应的unicode字符
# d,十进制整数
# o,将10进制整数自动转换成8进制表示然后格式化;
# x,将10进制整数自动转换成16进制表示然后格式化(小写x)
# X,将10进制整数自动转换成16进制表示然后格式化(大写X)

num = "numbers: {:b},{:o},{:d},{:x},{:X}, {:%}".format(15, 15, 15, 15, 15, 15.87623)
print(num)

# 传入“ 浮点型或小数类型 ”的参数
# e, 转换为科学计数法(小写e)表示,然后格式化;
# E, 转换为科学计数法(大写E)表示,然后格式化;
# f , 转换为浮点型(默认小数点后保留6位)表示,然后格式化;
# F, 转换为浮点型(默认小数点后保留6位)表示,然后格式化;
# g, 自动在e和f中切换
# G, 自动在E和F中切换
# %,显示百分比(默认显示小数点后6位)

 

标签:格式化,name,age,print,am,msg,字符串,yoki
来源: https://www.cnblogs.com/yuqi-yoki/p/16492258.html