Open In App

Printing String with double quotes - Python

Last Updated : 11 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Printing a string with double quotes means displaying a string enclosed in double quotes (") as part of the output. This can be helpful when we want to make it clear that the text itself is a string or when quotes are essential to the context.

Using Escape Characters (\")

Escape the double quotes inside the string to include them in the output.

Python
print("She said, \"Python is amazing!\"")

Output
She said, "Python is amazing!"

Explanation

  • Escape Character (\"): backslash (\) is used as an escape character to include double quotes inside the string. Without the backslash, the double quotes would terminate the string, causing a syntax error.
  • Output with Double Quotes: print() function processes the escaped double quotes and displays them as part of the output, making the string appear exactly as intended: She said, "Python is amazing!".

Using Triple Double Quotes (""")

Triple double quotes allow direct inclusion of double quotes without escaping.

Python
print("""She said, "Python is amazing!" """)

Output
She said, "Python is amazing!" 

Explanation:

  • Triple Double Quotes: String is enclosed in triple double quotes ("""), which allows the inclusion of double quotes (") within the string without the need for escape characters.
  • Direct Output: print() function directly outputs the string, including the double quotes around "Python is amazing!", without any special handling or escaping.

Using Triple Single Quotes (''')

Escape characters allow us to include special characters like double quotes (") in strings without ending them prematurely.

Python
print('''She said, "Python is amazing!" ''')

Output
She said, "Python is amazing!" 

Explanation

  • Triple Single Quotes: string is enclosed in triple single quotes ('''), which, like triple double quotes, allow for the inclusion of double quotes (") within the string without the need for escape characters.
  • Direct Output: print() function outputs the string exactly as it is, including the double quotes around "Python is amazing!", without needing any special syntax

Using String Formatting (f-strings)

When we enclose the string in single quotes ('), we can include double quotes inside it directly without escaping.

Python
quote = '"Python is amazing!"'
print(f"She said, {quote}")

Output
She said, "Python is amazing!"

Explanation:

  • Using an f-string for String Interpolation: f-string (formatted string literal) is used to embed the value of the quote variable directly into the string, where {quote} is replaced with the value stored in quote.
  • Directly Including Double Quotes: Variable quote contains the string with double quotes around "Python is amazing!" and when printed, the double quotes are preserved as part of the output: She said, "Python is amazing!"

Next Article
Practice Tags :

Similar Reads