51学通信论坛2017新版

标题: 字符串format的常见操作 [打印本页]

作者: admin    时间: 2018-8-4 23:11
标题: 字符串format的常见操作
以下在python3.6测试通过:
  1. print("{}的{}次方的最后2位是{}".format(2,10,pow(2,10,100)))
  2. print("{1}的{0}次方的最后2位是{2}".format(10,2,pow(2,10,100)))
  3. s='等级考试'
  4. print("{:25}".format(s))   #左对齐,默认
  5. print("{:^25}".format(s))  #居中对齐,默认
  6. print("{:>25}".format(s))  #右对齐
  7. print("{:*^25}".format(s))  #居中对齐且填充21个*号
  8. print("{:+^30}".format(s)) #居中且填充26个+号
  9. print("{:中^35}".format(s))  #居中且填充31个汉字”中"
  10. print("{:^1}".format(s))
  11. print("{:^8}".format(s))
  12. print("{:-^25,}".format(1234567890))
  13. print("{:.2f}".format(12345.89612))
  14. print("{:>25.3f}".format(12345.6789))
  15. print("{:.5}".format('全国计算机等级考试'))
  16. print("{:b},{:c},{:d},{:o},{:x},{:X}".format(65,65,65,65,65,65)) 按整数类型的6种类型进行格式化。例如:b是以二进制输出,:c是输出整数对应的unicode字符。  
  17. print("{:.2%}".format(0.56789))  #按百分比显示,保留2位。
复制代码
执行结果:
  1. 2的10次方的最后2位是24
  2. 2的10次方的最后2位是24
  3. 等级考试
  4.           等级考试
  5.                      等级考试
  6. **********等级考试***********
  7. +++++++++++++等级考试+++++++++++++
  8. 中中中中中中中中中中中中中中中等级考试中中中中中中中中中中中中中中中中
  9. 等级考试
  10.   等级考试
  11. ------1,234,567,890------
  12. 12345.90
  13.                 12345.679
  14. 全国计算机
  15. 1000001,A,65,101,41,41
  16. 56.79%
复制代码





欢迎光临 51学通信论坛2017新版 (http://bbs.51xuetongxin.com/) Powered by Discuz! X3