Categories
Python Answers

How to use quotation marks inside quotation marks with Python?

Spread the love

Sometimes, we want to use quotation marks inside quotation marks with Python.

In this article, we’ll look at how to use quotation marks inside quotation marks with Python.

How to use quotation marks inside quotation marks with Python?

To use quotation marks inside quotation marks with Python, we can wrap single quotes around double quotes and vice versa, or we can escape single quotes in single quotes or escape double quotes in double quotes.

We can also use quotes within triple quoted strings.

For instance, we write:

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

In the first 2 strings, we wrap single quotes around double quotes and vice versa.

We escaped the quotes in the last 3 strings.

Therefore, we should see:

"A word that needs quotation marks"
'A word that needs quotation marks'
"A word that needs quotation marks"
'A word that needs quotation marks'
"A word that needs quotation marks"

printed.

Conclusion

To use quotation marks inside quotation marks with Python, we can wrap single quotes around double quotes and vice versa, or we can escape single quotes in single quotes or escape double quotes in double quotes.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

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