首页 > 解决方案 > 如何在 Python 中打印带引号的字符串和变量?

问题描述

我需要在一行中完成。有 2 个标签:

      Albert Einstein once said, "A person who never made a mistake never tried anything new."

编码

famous_person = "Albert Einstein"
message ='\t\tf{famous_person} once said, "A person who never made a mistake never tried anything new."'
print(message)

f{}我猜是行不通的:(

标签: pythonstringapostrophe

解决方案


你可以。将f字符放在字符串之前,例如f'...{...}...',不是'f{...}'

famous_person = "Albert Einstein"
message =f'\t\t{famous_person} once said, "A person who never made a mistake never tried anything new."'
print(message)

'\t\tAlbert Einstein once said, "A person who never made a mistake never tried anything new."'

参考
https://www.python.org/dev/peps/pep-0498/


推荐阅读