문제

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 "'".

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top