# python 字符串格式化 msg = 'I am %s my hobby is music' % 'jake' # % 后面加s表示该位置是字符串 print(msg) basic = "my name is %s age %d" # %后门加d 表示该位置是数字 print(basic % ("tom", 10)) msg1 = "I am {0} my age {1}".format("lufei", 19) # 可以把参数理解为列表,前面的参数表示索引取值 print(msg1) msg2 = "I am {0} my age {0}".format("lufei", 19) print(msg2) msg3 = "I am {name} age {age}".format(name="jake", age="20") # 参数替换形式 print(msg3) msg3_1 = "I am {name:s} age {age:d}".format(name="jake", age=20) # 参数替换形式 print(msg3_1) vv = {"name": "suolong", "age": 19} msg4 = "I am {name} age {age}".format(**vv) # 传入字典前面需要加*号 msg5 = "I am {:s} age {:d} money{:f}".format("namei", 24, 189.9) # {:s} 这种写法的含义与 %s的用法类似 print(msg5) msg6 = "I am {:s} age {:d} money{:f}".format(*["shanzhi", 35, 8]) # 传入列表 print(msg6) msg7 = "member:{:b}*******{:o}******{:d}*******{:x}******{:X}********{:%}".format(15, 15, 15, 15, 15, 15, 15) print(msg7) # 转换二进制 转换八进制 转换16进制小写 转换16进制大写 转换成百分号的数据