51学通信论坛2017新版
标题:
字符串format的常见操作
[打印本页]
作者:
admin
时间:
2018-8-4 23:11
标题:
字符串format的常见操作
以下在python3.6测试通过:
print("{}的{}次方的最后2位是{}".format(2,10,pow(2,10,100)))
print("{1}的{0}次方的最后2位是{2}".format(10,2,pow(2,10,100)))
s='等级考试'
print("{:25}".format(s)) #左对齐,默认
print("{:^25}".format(s)) #居中对齐,默认
print("{:>25}".format(s)) #右对齐
print("{:*^25}".format(s)) #居中对齐且填充21个*号
print("{:+^30}".format(s)) #居中且填充26个+号
print("{:中^35}".format(s)) #居中且填充31个汉字”中"
print("{:^1}".format(s))
print("{:^8}".format(s))
print("{:-^25,}".format(1234567890))
print("{:.2f}".format(12345.89612))
print("{:>25.3f}".format(12345.6789))
print("{:.5}".format('全国计算机等级考试'))
print("{:b},{:c},{:d},{:o},{:x},{:X}".format(65,65,65,65,65,65)) 按整数类型的6种类型进行格式化。例如:b是以二进制输出,:c是输出整数对应的unicode字符。
print("{:.2%}".format(0.56789)) #按百分比显示,保留2位。
复制代码
执行结果:
2的10次方的最后2位是24
2的10次方的最后2位是24
等级考试
等级考试
等级考试
**********等级考试***********
+++++++++++++等级考试+++++++++++++
中中中中中中中中中中中中中中中等级考试中中中中中中中中中中中中中中中中
等级考试
等级考试
------1,234,567,890------
12345.90
12345.679
全国计算机
1000001,A,65,101,41,41
56.79%
复制代码
欢迎光临 51学通信论坛2017新版 (http://bbs.51xuetongxin.com/)
Powered by Discuz! X3