Quotes within quotes

Sanjana S

In Python 3.x:

    print(""s"")       # SyntaxError
    print("""s""")     # prints s
    print(""""s"""")   # SyntaxError
    print("""""s""""") # prints ""s

What is the reason behind this different behaviour, when there are different numbers of double quotes in the string?

thefourtheye

In Python you can create multiline strings with """...""". Quoting the documentation for strings,

String literals can span multiple lines. One way is using triple-quotes: """...""" or '''...'''.

In your first case, ""s"" is parsed like this

"" (empty string literal)  s  "" (empty string literal)

Now, Python doesn't know what to do with s. That is why it is failing with SyntaxError.

In your third case, the string is parsed like this

"""  "s  """ (End of multiline string)  `"`

Now the last " has no matching ". That is why it is failing.

In the last case, """""s""""" is parsed like this

"""  ""s  """  ""

So, the multiline string is parsed successfully and then you have an empty string literal next to it. In Python, you can concatenate two string literals, by writing them next to each other, like this

print ("a" "b")
# ab

So, the last empty string literal is concatenated with the ""s.

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Regex for single quote within single quotes

분류에서Dev

matching random sentences within quotes with regular expression for sublime text

분류에서Dev

Why does $_.Name include full path when within quotes in Powershell?

분류에서Dev

sed command string variable concatenation within single quotes

분류에서Dev

How do I print a character variable within quotes using php function

분류에서Dev

Using regexp_replace to replace single quotes around string but not within words e.g. can't

분류에서Dev

Double quotes in bash

분류에서Dev

String initialization with double quotes

분류에서Dev

call a function with single quotes

분류에서Dev

Extract everything between quotes

분류에서Dev

Match a given sequence only if not between quotes, taking escaped quotes into consideration

분류에서Dev

Ansible replace single quotes regexp

분류에서Dev

Javascript regex for single, double and no quotes

분류에서Dev

Echoing functions, but not enough quotes available

분류에서Dev

Quotes Messing Up Python Scraper

분류에서Dev

Express req.body is in quotes

분류에서Dev

String Tokenizer (Double Quotes and Whitespace)

분류에서Dev

SQL injection without single quotes

분류에서Dev

Remove spaces (apostrophes) around quotes with regex in ruby

분류에서Dev

Dollar sign interpolation inside quotes in bash

분류에서Dev

What characters need to be escaped in files without quotes?

분류에서Dev

SoapUI Escaped Quotes Cause Assertion Failures

분류에서Dev

Double quotes and variables in Sed - Unknown expression?

분류에서Dev

Ignoring a Rogue quote inside Double quotes

분류에서Dev

Regex to Match Quoted Strings Ignoring Double Quotes

분류에서Dev

Variable containing single quotes just disappears

분류에서Dev

Putting a line of text into an array delimited by quotes?? Delphi

분류에서Dev

Java copy entire file without the double quotes

분류에서Dev

Double-quotes in a Javascript generated string issue

Related 관련 기사

  1. 1

    Regex for single quote within single quotes

  2. 2

    matching random sentences within quotes with regular expression for sublime text

  3. 3

    Why does $_.Name include full path when within quotes in Powershell?

  4. 4

    sed command string variable concatenation within single quotes

  5. 5

    How do I print a character variable within quotes using php function

  6. 6

    Using regexp_replace to replace single quotes around string but not within words e.g. can't

  7. 7

    Double quotes in bash

  8. 8

    String initialization with double quotes

  9. 9

    call a function with single quotes

  10. 10

    Extract everything between quotes

  11. 11

    Match a given sequence only if not between quotes, taking escaped quotes into consideration

  12. 12

    Ansible replace single quotes regexp

  13. 13

    Javascript regex for single, double and no quotes

  14. 14

    Echoing functions, but not enough quotes available

  15. 15

    Quotes Messing Up Python Scraper

  16. 16

    Express req.body is in quotes

  17. 17

    String Tokenizer (Double Quotes and Whitespace)

  18. 18

    SQL injection without single quotes

  19. 19

    Remove spaces (apostrophes) around quotes with regex in ruby

  20. 20

    Dollar sign interpolation inside quotes in bash

  21. 21

    What characters need to be escaped in files without quotes?

  22. 22

    SoapUI Escaped Quotes Cause Assertion Failures

  23. 23

    Double quotes and variables in Sed - Unknown expression?

  24. 24

    Ignoring a Rogue quote inside Double quotes

  25. 25

    Regex to Match Quoted Strings Ignoring Double Quotes

  26. 26

    Variable containing single quotes just disappears

  27. 27

    Putting a line of text into an array delimited by quotes?? Delphi

  28. 28

    Java copy entire file without the double quotes

  29. 29

    Double-quotes in a Javascript generated string issue

뜨겁다태그

보관