Use another quotation mark, just like below:
foo = ["'the"]
foo = ['"the']
foo = ['''"the''']
foo = ["""'the"""]
or use '\'
Вопрос
I searched around the web but couldn't find an answer. How can someone add a single quote as part of an element of a list in python? For example,
foo = ['the']
simple enough. But what if I want something like this?
foo = [''the']
where the element is 'the, with the single quotation appended?
Решение
Use another quotation mark, just like below:
foo = ["'the"]
foo = ['"the']
foo = ['''"the''']
foo = ["""'the"""]
or use '\'
Другие советы
There are two ways of representing strings in Python (to avoid this issue):
some_string = '...' # single quotes
some_string = "..." # double quotes
Therefore, you can use the second one:
foo = ["'the"]
You can also escape the '
character:
foo = ['\'the']
Either use double-quotes when defining the string containing a single-quote character, or use single-quotes, and \
-escape the inner single-quote:
"'the"
'\'the'
both work.
(Your question has got nothing to do with lists, only the string in your list...)
Escape the single quote '\''
or surround the string in double quotes "'"
.