如何使 f 字符串区分整数和字符串?

How do I make an f-string distinguish between an integer and a string?

两者

x = 1
f"{x}"

x = '1'
f"{x}"

给予

'1'

作为输出。如何获得 "1""'1'" 作为输出?

使用 !r 修饰符获取对象 repr 而不是它的 str:

f"{x!r}"

如果你想得到特定的输出来读取:
"'1'"(带引号的文本)和
"1"(不带引号的文本)
你可以使用:

f"{x}"     #text without single quotes
f"'{x}'"   #text with single quotes

这仅在您交替引用时有效。像这样:" ' ' " , ' " " '

f'"{x}"'

结果:“1”作为输出