21、处理多行f-string(换行符的处理)
可以用换行符\n来打印多行文本。
>>> multi_line = (f'R: {color["R"]}\nG: {color["G"]}\nB: {color["B"
...: ]}\n')
>>> multi_line
'R: 123\nG: 145\nB: 255\n'
>>> print(multi_line)
R: 123
G: 145
B: 255
还可以用三引号实现多行字符串。这样不单能增加换行符,还能有Tab。
>>> other = f"""R:{color["R"]}
...: G:{color["G"]}
...: B:{color["B"]}
...:"""
>>> print(other)
R: 123
G: 145
B: 255
用 Tab 的代码样例
>>> other = f'''
...:this is an example
...:
...:^Iof color {color["R"]}
...:
...: '''
>>> other
'\nthis is an example\n\n\tof color 123\n \n'
>>> print(other)
this is an example
of color123
>>>