Ways to use double quotes inside a string variable in Python

To use double quotes inside a string variable in Python, see below for the three ways:

1) Use single and double quotes together:

>>> print '"words to be double-quoted"'
"words to be double-quoted"

2) Escape the double quotes within the string:

>>> print "\"words to be double-quoted\""
"words to be double-quoted" 

3) Use triple-quoted strings:

>>> print """ "words to be double-quoted" """
"words to be double-quoted" 

Note: when you need to use single quote within a single quote, the above methods work for single quote case as well.

References:

Leave a Reply

Your email address will not be published. Required fields are marked *