print(“print(“Hello World!”) doesn’t work”)

huangapple go评论67阅读模式
英文:

print("print("Hello World!")") doesn't work

问题

我尝试使用这段代码 print("print("Hello World!")"),但它给我报错。

我以为它会在控制台打印出这样的内容:

print("Hello World!")

但它没有工作。我认为这可能与括号或 " 这些字符有关,但我无法弄清楚。感谢您的帮助。

英文:

I tried to use this code print("print("Hello World!")") but it gives me an error.

I thought it would print on the console this:

> print("Hello World!")

but it didn't work. I think it may be a problem about brackets or " this but i can't figure it out. Thanks for your help.

答案1

得分: 1

以下是翻译好的部分:

如果您想要一个更通用的答案,演示如何对任意深度的事物进行处理,我会选择转义的反斜杠引号。

  • 第一:"
  • 第二:\"
  • 第三:\\\"
  • 第四:\\\\\\\"

关键在于除了在每个嵌套级别最终使用\"来转义双引号之外,您还需要转义前一级的反斜杠。

以下是一些演示:

print("print(\"Hello World!\")")
print("print(\"print(\\\"Hello World!\\\")\")")
print("print(\"print(\\\"print(\\\\\\\"Hello World!\\\\\\\")\\\")\")")
exec("exec(\"exec(\\\"print(\\\\\\\"Hello World!\\\\\\\")\\\")\")")

这应该给您:

print("Hello World!")
print("print(\"Hello World!\")")
print("print(\"print(\\\"Hello World!\\\")\")")
Hello World!
英文:

If you want a more generic answer that demonstrates how to do things to an arbitrary depth, I would go with escaped backslash quotes.

  • First.: "
  • Second: \"
  • Third: \\\"
  • fourth: \\\\\\\"

The key is that in addition to escaping the double quote ultimately with \" at each level of nesting you need to escape the prior level's backslashes.

Here are some demos:

print("print(\"Hello World!\")")
print("print(\"print(\\\"Hello World!\\\")\")")
print("print(\"print(\\\"print(\\\\\\\"Hello World!\\\\\\\")\\\")\")")
exec("exec(\"exec(\\\"print(\\\\\\\"Hello World!\\\\\\\")\\\")\")")

That should give you:

print("Hello World!")
print("print(\"Hello World!\")")
print("print(\"print(\\\"Hello World!\\\")\")")
Hello World!

答案2

得分: 0

现在您可以使用“三引号”:

s = """print('print("Hello World!")')"""
print(s)

按要求输出

英文:

So now you can use triple quotes:

s = """print('print("Hello World!")')"""
print(s)

Output as requested

答案3

得分: 0

如果你想指定 `print()` 嵌套的深度比如说用一个单独的 `"` 标记你可以使用这个

```python
def surround_in_print(msg, depth):
    if depth == 0:
        return msg
    return f'print("{surround_in_print(msg, depth-1)}")'

print(surround_in_print("Hello World!", 5))

输出:

print("print("print("print("print("Hello World!")")")")")

如果你想使用一个任意序列的类似引号的标记,你可以使用这个:

def surround_in_print_with_quotes(msg, quotes):
    if not quotes:
        return msg
    
    q, *rest = quotes
    return f'print({q}{surround_in_print_with_quotes(msg, rest)}{q})'

first = ''"''
second = '"'"'
third = ''"""''
fourth = '"?"'
print(surround_in_print_with_quotes("Hello World!", (first, second, third, fourth)))

输出:

print("print('print("""print(?Hello World!?)""")')")
英文:

If you want to specify a depth to which the print()s are nested with, say, a single " mark, you could use this:

def surround_in_print(msg, depth):
    if depth == 0:
        return msg
    return f'print("{surround_in_print(msg, depth-1)}")'

print(surround_in_print("Hello World!", 5))

Output:

print("print("print("print("print("Hello World!")")")")")

If you wanted to use an arbitrary sequence of quote-like marks you could use this:

def surround_in_print_with_quotes(msg, quotes):
    if not quotes:
        return msg
    
    q, *rest = quotes
    return f'print({q}{surround_in_print_with_quotes(msg, rest)}{q})'

first = '"'
second = "'"
third = '"""'
fourth = "?"
print(surround_in_print_with_quotes("Hello World!", (first, second, third, fourth)))

Output:

print("print('print("""print(?Hello World!?)""")')")

huangapple
  • 本文由 发表于 2023年6月30日 03:32:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76584118.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定