Print double quotes in a string in Python

As programmers, we have all struggled with strings at some point. It may be when we loop through its characters, concatenate them with other data types, or even when we just need to print them out. In this tutorial, we will explore one such problem of printing out double quotes in Python.

Suppose we want to print the following statement- They said, “We study with Codespeedy!”. And we code it like we usually print strings.

print("They said, "We study with Codespeedy!"")

Output:

 print("They said, "We study with Codespeedy!"")
^^^^^^^^^^^^^
SyntaxError: invalid syntax.

Whoops! We got an error. Here, Python gets confused between the syntax of a string that includes double quotes and the double quotes that we want to display.

Most of the time, there exists differences between the string literal and the string value. String literal is the code we write- including all characters and quotations, and string value is the output we get. To ensure the correct string value is displayed, we must modify the string literal accordingly.

Let us explore several workarounds to escape double quotes while printing in Python.

Solutions to print double quotes in Python

We will see multiple solutions one by one.

Solution 1: Using single quotes

Single quotes can also be used as string delimiters in Python just as double quotes. Thus, they function the same as double quotes in the print method. Simply replace the double quotes with the single quotes.

print('They said, "We study with Codespeedy!"')

Output:

They said, "We study with Codespeedy!"

Solution 2: Using an escape character

An escape character (here, backslash) indicates that the character should be interpreted differently. Hence, adding a backslash right before the quote will print the quote.

print("They said, \"We study with 'Codespeedy!'\"")

Output:

They said, "We study with 'Codespeedy!'"

Solution 3: Using triple quotes

Triple quotes are commonly used for multiline printing in Python. They can also be applied to escape double quotes. You can use either triple single quotes or triple double quotes.

print("""They said, "'Codespeedy' makes learning fun and easy. 
They value the journey of every learner, 
& work to create a supportive platform for growth/skill development."
""")

Output:

They said, "'Codespeedy' makes learning fun and easy. 
They value the journey of every learner, 
& work to create a supportive platform for growth/skill development."

Examples

Following are some scenarios where we might need to print double quotes. In these examples, at least one of the above methods is implemented.

Example 1: String Variables

Formatted String (F-string)

variable = '"We study with Codespeedy!"'
print(f"They said, {variable}")

Note: F-strings are available from Python 3.6 onwards.

String Formatting

variable = '"We study with Codespeedy!"'
print("They said, %s" % variable)
variable = '"We study with Codespeedy!"' 
print("They said, {}" .format(variable))

String Concatenation

string1="They said, "
string2='"We study with Codespeedy!"'
print(string1 + string2)

Example 2: Escape Sequences

In the solution above, we used an escape character to print double quotes. Apart from that, we can even use an escape sequence which is an escape character together with one or more characters.

Unicode Escape sequence

print("They said, \u0022We study with Codespeedy!\u0022")

ASCII Escape Sequence

print("They said, \x22We study with Codespeedy!\x22")

Example 3: Raw String

print(r'They said, "We study with Codespeedy!"')

Conclusion

This tutorial discussed three common solutions to a frequently occurring problem and additional examples related to these solutions. There can be many approaches to solving a problem. We must choose the one that best suits our requirements and code efficiency. Happy Learning!

Leave a Reply

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