简体   繁体   中英

How to print a string that contains quotes in python

I want to print quotes in python. Is it possible to print a " in python or any other language? I tried print("""") . Here a & b are useless because it was giving inappropriate grammar error.

a = 3
b ="cd"
print("""")

There are a few ways:

Use single and double quotes together:

You are allowed to start and end a string literal with single quotes (also known as apostrophes), like 'blah blah'. Then, double quotes can go in between,

print ('"quotation marks"')
"quotation marks"

Escape the double quotes within the string:

You can put a backslash character followed by a quote (\\" or \\'). This is called an escape sequence and Python will remove the backslash, and put just the quote in the string.

print ("\"quotation marks\"")
"quotation marks" 

Use triple-quoted strings:

print (""" "quotation marks" """)
"quotation marks" 

Wrap it in single quotes.

print('AAA " AAA " AAA')
# => AAA " AAA " AAA

Or escape the quotes:

print("AAA \" \" AAA ")
# => AAA " " AAA
print(' \' ') # works with any quotes print(" \" ") # works with any quotes print(' " ') # only works for opposite quotes. print(" ' ") # only works for opposite quotes.

You can print a quote by escaping it with the \\ character.

print("\"")

The \\ will cancel the next symbols syntactical meaning an use it as a character.

print('""""')

or

print("\"\"\"\"")

or

print('"'*4)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM