age = 20
name = 'Swaroop'
"""
Swaroop was 20 years old when he wrote this book
"""
print('{0} was {1} years old when he wrote this book'.format(name, age))
"""
Why is Swaroop playing with that python?
"""
print('Why is {0} playing with that python?'.format(name))
"""
注意数字只是一个可选选项,所以你同样可以写成
Swaroop was 20 years old when he wrote this book
"""
print('{} was {} years old when he wrote this book'.format(name, age))
"""
Why is Swaroop playing with that python?
"""
print('Why is {} playing with that python?'.format(name))
"""
对于浮点数 '0.333' 保留小数点(.)后三位
0.333
"""
print('{0:.3f}'.format(1.0 / 3))
"""
使用#填充文本,并保持文字处于中间位置
使用 (^) 定义 '###hello###'字符串长度为 11
'####hello###'
"""
print("{0:#^11}".format("hello"))
"""
基于关键词输出 'Swaroop wrote A Byte of Python'
Swaroop wrote A Byte of Python
"""
print("{0} wrote {1}".format("Swaroop", "A Byte of Python"))
"""
Swaroop wrote A Byte of Python
"""
print("{} wrote {}".format("Swaroop", "A Byte of Python"))
"""
Swaroop wrote A Byte of Python
"""
print("{name} wrote {book}".format(name="Swaroop", book="A Byte of Python"))
"""
注意 print 总是会以一个不可见的“新一行”字符(\n)结尾,因此重复调用 print将会在相互独立的一行中分别打印。
为防止打印过程中出现这一换行符,你可以通过 end 指定其应以空白结尾
a bc de f
"""
print("a", end=" ");
print("b", end="");
print("c", end=" ");
print("d", end="");
print("e", end=" ");
print("f");
"""
转义字符
' "
"""
print('\ \n ' "')
"""
This is the first line
This is the second line
"""
print("This is the first line\nThis is the second line")
"""
如果你需要指定一些未经过特殊处理的字符串,比如转义序列,那么你需要在字符串前增加 r 或 R 来指定一个 原始(Raw) 字符串
Newlines are indicated by \n
"""
print(r"Newlines are indicated by \n")
"""
Newlines are indicated by \n
"""
print(R"Newlines are indicated by \n")
"""
显式行连接
This is a string. This continues the string.
"""
s = 'This is a string.
This continues the string.'
print(s)
"""
等同于i = 5
"""
i =
5
网友评论