Search code examples
pythondouble-quotessingle-quotes

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.


Solution

  • 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.