python - 如何防止关闭“变量

标签 python string

我有一个变量

sentence = "In 1794, shortly after his arrival in Manchester, Dalton was elected a member of the Manchester Literary and Philosophical Society, the "Lit & Phil", and a few weeks later he communicated his first paper on "Extraordinary facts relating to the vision of colours", in which he postulated that shortage in colour perception was caused by discoloration of the liquid medium of the eyeball. In fact, a shortage of colour perception in some people had not even been formally described or officially noticed until Dalton wrote about his own. Since both he and his brother were colour blind, he recognized that this condition must be hereditary."

现在这可能同时具有 ""和 '',因此它将关闭变量的值。我想阻止这种情况。还有其他存储字符串的方法吗?

最佳答案

使用 \ 转义嵌入的引号:

"Some text with \"embedded\" quotes"

如果您的文本只包含双引号,您可以使用单引号而不用转义双引号:

'Some text with "embedded" quotes'

最后但并非最不重要的一点是,您可以三倍外引号并避免自己也不得不转义换行符:

"""Some text with "embedded" quotes"""
"""Some text with "embedded" quotes
and a newline too"""

对于您的示例,单引号已经可以解决问题:

sentence = 'In 1794, shortly after his arrival in Manchester, Dalton was elected a member of the Manchester Literary and Philosophical Society, the "Lit & Phil", and a few weeks later he communicated his first paper on "Extraordinary facts relating to the vision of colours", in which he postulated that shortage in colour perception was caused by discoloration of the liquid medium of the eyeball. In fact, a shortage of colour perception in some people had not even been formally described or officially noticed until Dalton wrote about his own. Since both he and his brother were colour blind, he recognized that this condition must be hereditary.'

关于python - 如何防止关闭“变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18270197/

相关文章:

python - Flask:在 View 中获取蓝图的 url_prefix

python - 为什么我的 json 中是 '/'

c - 读取未知大小的字符串

string - XQuery - 使用 sum() 返回字符串值 NaN

python - 将列表字典转换为字典列表

python - `Psycopg` 这个名字从何而来?

python - 函数调用执行速度比非函数调用快

C++:覆盖缓存中的 std::string

带有正则表达式的 Java String.split()

python - 将重复变量插入字符串