f'...' 是一种 Python 中用于格式化字符串的方式,可以在字符串中插入变量和表达式。在 f'...' 中,可以使用花括号 {} 来引用变量和表达式,例如:

name = 'Tom'
age = 18
print(f'My name is {name}, and I am {age} years old.')

输出:My name is Tom, and I am 18 years old.

而 '''...''' 则是 Python 中多行字符串的表示方式,可以方便地表示包含多行文本的字符串,例如:

text = '''
This is a
multi-line
text.
'''
print(text)

输出:

This is a
multi-line
text.

因此,f'...' 和 '''...''' 的作用不同,不能互相替代。

Python 中 f'...' 和 '''...''' 的区别:格式化字符串 vs 多行字符串

原文地址: https://www.cveoy.top/t/topic/nipo 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录