
Python
、
NumPy
、
matplotlib
和
pandas
|
339
将
str
对象的子集组合为一个新的。
打印结果。
字符串替换通常用于参数化文本输出。
In [25]: repl = 'My name is %s, I am %d years old and %4.2f m tall.'
In [26]: print(repl % ('Gordon Gekko', 43, 1.78))
My name is Gordon Gekko, I am 43 years old and 1.78 m tall.
In [27]: repl = 'My name is {:s}, I am {:d} years old and {:4.2f} m tall.'
In [28]: print(repl.format('Gordon Gekko', 43, 1.78))
My name is Gordon Gekko, I am 43 years old and 1.78 m tall.
In [29]: name, age, height = 'Gordon Gekko', 43, 1.78
In [30]: print(f'My name is {name:s}, I am {age:d} years old and \
{height:4.2f}m tall.')
My name is Gordon Gekko, I am ...