Escape double quote in a String
To escape double quote character in a string in Python, when the string is created using double quotes, place a backslash character before the double quote character inside the string.
In the following example, Welcome "Arjun"
is string value. We escape the double quote characters inside the string, as shown in the following.
Welcome \"Arjun\"
Program
In the following program, we create a string message
, with double quote character escaped using backslash character, and print this string to console output.
main.py
message = "Welcome \"Arjun\""
print(message)
Output
Welcome "Arjun"
If the string is defined using single quotes, then there is no need to escape a double quote. The double quote in this scenario is a valid character.
main.py
message = 'Welcome "Arjun"'
print(message)
Output
Welcome "Arjun"
Conclusion
In this Python Tutorial, we learned how to escape a double quote character inside a string, using backslash character.