gpt4 book ai didi

python - 在 python 中使用格式化打印的双引号与单引号

转载 作者:太空狗 更新时间:2023-10-30 00:39:27 32 4
gpt4 key购买 nike

所以我正在经历“艰难地学习 Python”

同时这样做:

formatter = "%r %r %r %r"

print formatter % (
"I had this thing.",
"That you could up right.",
"But it didn't sing.",
"So I said goodnight"
)

输出是

'I had this thing.' 'That you could up right.' "But it didn't sing." 'So I said goodnight'

但我不确定为什么第 3 根弦有双弦。

最佳答案

"a"'a' 是相同的字符串,没有区别。

第三个字符串包含一个撇号,所以它不能表示为 'But it didn't sing.' 因为那会在 didn 之后结束字符串并引发一个语法错误

如果你想用单引号表示一个字符串,你可以这样做:

"'"

'\''

与双引号相同:

'"'

"\""

如果你有一个包含两个引号的字符串,你可以选择一个:

'"\'"

"\"'"

关于python - 在 python 中使用格式化打印的双引号与单引号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34607165/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com