pycharm练习“字符转义”:任务完成但语法错误

2024-10-01 09:38:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我遵循PyCharm Edu版本中的内置教程,我一直在研究字符串-字符转义。 在练习中,我被要求打印以下内容: 这个冰淇淋的名字叫“Sweeet'n'Tasty” 通过使用字符转义,我的代码是:

print("The name of this ice-cream is \"Sweeet\'n\'Tasty\"")

它仍然给我“抱歉,打印错误的字符串”。老实说,我不认为我打印错了线。有什么帮助吗?在


Tags: ofthe字符串代码name版本教程名字
3条回答

最后一个print语句使用单引号,因此测试希望您只转义围绕“n”的单引号,因此这行代码适用于我:

print('The name of this ice-cream is "Sweeet\'n\'Tasty"')

你必须逃避“因为你在你的印刷品中使用它,但你的'不需要保护。在

打印“\'n”和“'n”将输出相同的行,但是转义,即使不可见,也会生成一些由您的运动控制器读取的内容。在

尝试删除'

print("The name of this ice-cream is \"Sweeet'n'Tasty\"")

对于包含“or”的字符串,另一种解决方案是使用三元组,如下所示:

^{pr2}$

在这种情况下,事实上,刑期的终止是由一种“武力来保护它,但中间的”并不需要被保护。在

你也可以颠倒使用'和'来保护'或

print('The name of this ice-cream is "Sweeet\'n\'Tasty"')

也可以使用3':

print('''The name of this ice-cream is "Sweeet'n'Tasty"''')

如果这仍然不起作用,你能提供断言测试吗?在

编辑:

这似乎是您面临的问题:http://iswwwup.com/t/d08b1b05234e/print-out-text-using-one-string-in-python.html

来自一个模棱两可的测试需求/IDE行为。在

dont_worry = "Don't worry about apostrophes"
print(dont_worry)
print("The name of this ice-cream is \"Sweeet\"")
print("The name of this ice-cream is \"Sweeet'n'Tasty\"")


打印(“这个冰淇淋的名字是\“sweet'n'Tasty\”)

相关问题 更多 >