jxie0755
jxie0755

Reputation: 1742

How do I represent " as a string in python without using '

As you know that python support both ' and " for string like

a = 'str'
b = "str"

If I want to represent the symbol ", I can use:

a = '"'

However, I don't want to use ', is there any way? In java, which does not allow ' for string representing, I can use:

a = "\""

but this escape won't work in python

print("\"") # >>> \"

Thanks

Upvotes: 0

Views: 161

Answers (2)

Adam Smith
Adam Smith

Reputation: 54213

If you cannot use ' and must use raw strings, then there is no way to express a single double quotation mark " as a string.

There are two methods of including a string delimiter (' or ") in a string:

  1. Use the other delimiter for the string, e.g. '"' or "'"
  2. Escape the character with a backslash, e.g. "\"" or '\''

Your constraints remove both of those possibilities.

Upvotes: 0

Aakash Mehta
Aakash Mehta

Reputation: 29

you can perhaps use a triple quoted string

print("""I wasn't, I shan't. "This is a quote".""")

your output should be

I wasn't, I shan't. "This is a quote".

Upvotes: 1

Related Questions