1、常用百分号格式化方式:
STRING = "I am %s" %"alex" STRING = "I am %s age %d" %("alex", 18) STRING = "I am %(name)s age %(age)d" %({"name": "alex", "age": 18}) STRING = "percent %.2f" %99.97623 STRING = "I am %(pp).2f" %{"pp": 123.425556} STRING = "I am %(pp).2f %%" %{"pp": 123.425556}
2、常用format格式化方式:
STRING = "I am {}, age {}, {}".format("seven", 18, 'alex') STRING = "I am {}, age {}, {}".format(*["seven", 18, 'alex']) STRING = "I am {0}, age {1}, really {0}".format("seven", 18) STRING = "I am {0}, age {1}, really {0}".format(*["seven", 18]) STRING = "I am {name}, age {age}, really {name}".format(name="seven", age=18) STRING = "I am {name}, age {age}, really {name}".format(**{"name": "seven", "age": 18}) STRING = "I am {0[0]}, age {0[1]}, really {0[2]}".format([1, 2, 3], [11, 22, 33]) STRING = "I am {:s}, age {:d}, money {:f}".format("seven", 18, 88888.1) STRING = "I am {:s}, age {:d}".format(*["seven", 18]) STRING = "I am {name:s}, age {age:d}".format(name="seven", age=18) STRING = "I am {name:s}, age {age:d}".format(**{"name": "seven", "age": 18}) STRING = "numbers: {:b},{:o},{:d},{:x},{:X}, {:%}".format(15, 15, 15, 15, 15, 15.87623, 2) STRING = "numbers: {:b},{:o},{:d},{:x},{:X}, {:%}".format(15, 15, 15, 15, 15, 15.87623, 2) STRING = "numbers: {0:b},{0:o},{0:d},{0:x},{0:X}, {0:%}".format(15) STRING = "numbers: {num:b},{num:o},{num:d},{num:x},{num:X}, {num:%}".format(num=15)
3、python脚本指定编码的两种方式
#!/usr/bin/python # coding=utf-8 #!/usr/bin/python # -*- coding: utf-8 -*-
参考链接:
https://www.cnblogs.com/czqq/p/6108557.html
https://www.cnblogs.com/exusll/p/6393621.html?utm_source=tuicool&utm_medium=referral #字符串操作
https://www.crifan.com/python_head_meaning_for_usr_bin_python_coding_utf-8/