1.有三种string formatting的方式
2.第一种方法,使用formatting with placeholders 占位符格式化 %s
print("I'm going to inject %s here." %'something')
I'm going to inject something here.
也可以使用tuple来解决多个要插入的情况:
print("I'm going to inject %s text here, and %s text here." %('some','more'))
也可以:
x, y = 'some', 'more'
print("I'm going to inject %s text here, and %s text here."%(x,y))
%s 字符串内容inject进去;%r把字符串一起inject进去 \t产生空格 但在%r里之后就不算特殊字符
%d是直接自取整数,不保留小数
关于保留多少位:
(1)%7.2f
表示输出精确到小数点后2位,总长度7位(含小数点),不够时左侧补空格。
(2)%10f
输出数字总位数为10位,小数点也占一位。不够时左侧补空格。
(3)%09.f
"09."表示只输出整数位,不够9位左侧补0;同理,"010."表示只输出整数位,不够10位左侧补0。
2.第二种方法 .format()的方法
print('This is a string with an {}'.format('insert'))
This is a string with an insert
这种方法相比于之前的%s格式输出有以下三个优点: