pythonsyntaxstring-literalsquoting

Using quotation marks inside quotation marks


When I want to do a print command in Python and I need to use quotation marks, I don't know how to do it without closing the string.

For instance:

print " "a word that needs quotation marks" "

But when I try to do what I did above, I end up closing the string and I can't put the word I need between quotation marks.

How can I do that?


Solution

  • You could do this in one of three ways:

    1. Use single and double quotes together:

      print('"A word that needs quotation marks"')
      "A word that needs quotation marks"
      
    2. Escape the double quotes within the string:

      print("\"A word that needs quotation marks\"")
      "A word that needs quotation marks" 
      
    3. Use triple-quoted strings:

      print(""" "A word that needs quotation marks" """)
      "A word that needs quotation marks"