Difference between using single and double quotes for strings


#1


In the list exercises, I noticed strings defined with both single and double quotation marks. Are the single quotation marks reserved for characters such as

listofhchars = ['a', 'b', 'c']

and the double quotations are reserved for strings such as

listofstrings = ["List item 1", "List item 2", "List item 3"]

I looked up the following reference but didn't understand the meaning:
python.org lexical_analysis

In plain English: Both types of literals can be enclosed in matching single quotes (') or double quotes ("). They can also be enclosed in matching groups of three single or double quotes (these are generally referred to as triple-quoted strings). The backslash () character is used to escape characters that otherwise have a special meaning, such as newline, backslash itself, or the quote character.

Bytes literals are always prefixed with 'b' or 'B'; they produce an instance of the bytes type instead of the str type. They may only contain ASCII characters; bytes with a numeric value of 128 or greater must be expressed with escapes.


#2

dealing with byte literals is not that common, you can delve into that when you really need it

you can use both, and it doesn't matter. The only thing which you should keep in mind is that you can use apostrophes in the string if you enclose it with qoutation marks:

"th'is"

and vice versa:

'"quote"'

otherwise, you need to escape it:

"\"quote\""

#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.