Having quotes as a character in a python list

How do I put quotes in a list as an actual character?

I am currently doing the following:

pairings = ["A,#","B,6","C,+","D,""]

      

As you can see for "D" I am making D equal to the quote, however this is not possible because Python thinks it is a different value in the list. If I wanted to make D equal to "on this list, how would I do it?"

+3


source to share


3 answers


For strings in python, you can use both single and double quota labels, '

and "

. So you can use one to have a string containing the other.

pairings = ['A,#','B,6','C,+','D,"']

      

I changed all the quotes to only use one, so it is consistent.



You can also escape quotes in strings by using backslashes as shown below

s = 'This is my string, it contains a single quotation mark \', but that\ fine because I\'ve escaped them!'

      

+1


source


You have 2 choces:

pairings = ['A,#','B,6','C,+','D,"']

      

or



pairings = ["A,#","B,6","C,+","D,\""]

      

In python, you can placa String inside 'or' qotation characters. Or you can try to avoid them.

0


source


You can use a forward slash before "I mean

"D,\" "

      

or

'D, " '

      

0


source







All Articles