简体   繁体   中英

Are elements separated in a list with a comma?

If I create a list in python like this:

list1 = ['a' 'b' 'c']

or

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

What is the difference between list1 and list2 ?

The difference is that list1 's value would be ['abc'] , while list2 's value would be ['a', 'b', 'c'] .

Observe:

>>> ['a' 'b' 'c']
['abc']
>>> ['a', 'b', 'c']
['a', 'b', 'c']
>>> 

This is due to the fact that adjacent string literals (delimited by whitespace), possibly using different quoting conventions, are allowed, and their meaning is the same as their concatenation. Thus, "hello" 'world' is equivalent to "helloworld" .

The above is a quote from the documentation: String literal concatenation

The first example creates a list with one element, ['abc'] . The second creates a list with 3 elements: ['a', 'b', 'c'] .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM