Monday 20 September 2021

Python: How to place double quotes in a string

There are two solutions to this problem.

 

Solution 1: Keep the string in single quotes and use double quote as it is.

 

Example

str1 = 'Hi, I am from "India"'

 

Solution 2: If you enclose the string in double quotes, use the escape (\) character to escape double quotes.

 

Example

str2 = "Hi, I am from \"India\""

esacpe_double_quote.py

str1 = 'Hi, I am from "India"'
str2 = "Hi, I am from \"India\""

print('str1', ' -> ' , str1)
print('str2', ' -> ' , str2)


Output

str1  ->  Hi, I am from "India"
str2  ->  Hi, I am from "India"





 

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment