简体   繁体   中英

Use single quote and double quote same time as string python

How can I use single quote and double quote same time as string python? For example:

string = "Let's print "Happines" out"

result should be Let's print "Happines" out

I tried to use backslash but it prints out a \\ before 's that should be.

In python there's lots of ways to write string literals.

For this example you can:

print('Let\'s print "Happiness" out')
print("Let's print \"Happiness\" out")
print('''Let's print "Happiness" out''')
print("""Let's print "Happiness" out""")

Any of the above will behave as expected.

Taking this string:

string = "Let's print "Happines" out"

If you want to mix quotes, use the triple single quotes:

>>> string = '''Let's print "Happines" out'''
>>> print(string)
Let's print "Happines" out

Using triple quotes is acceptable too:

>>> string = """Let's print "Happines" out"""
>>> print(string)
Let's print "Happines" out

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