Python | Printing String with double quotes

Many times, while working with Python strings, we have a problem in which we need to use double quotes in a string and then wish to print it. This kind of problem occurs in many domains like day-day programming and web-development domain. Lets discuss certain ways in which this task can be performed.

Method #1 : Using backslash (“\”)
This is one way to solve this problem. In this, we just employ a backslash before a double quote and it is escaped.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Printing String with double quotes
# Using backslash
  
# initializing string
# using backslash
test_str = "geeks\"for\"geeks"
  
# printing string
print("The string escaped with backslash : " + test_str)

chevron_right


Output :

The string escaped with backslash : geeks"for"geeks

 

Method #2 : Using triple quotes
This is one more way in python to print and initialize a string. Apart from multiline comment, triple quotes are also good way of escaping.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Printing String with double quotes
# Using triple quotes
  
# initializing string
# using triple quotes
test_str = """geeks"for"geeks"""
  
# printing string
print("The string escaped with triple quotes : " + test_str)

chevron_right


Output :

The string escaped with triple quotes : geeks"for"geeks
My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.