List, string, Liststring

Source: Internet
Author: User

List, string, Liststring

In List processing of Python, string seems to be a List composed of a single character ....

See the lst4 and lst6 operations in the following code.

Code
 # Coding: gb2312
Lst1 = [1, 2, 4]
Lst1.append ([5, 6, 7, 8])
Print lst1 # print the result: [1, 2, 3, 4, [5, 6, 7, 8]

Lst2 = [1, 2, 4]
Lst2.append( "5678 ")
Print lst2 # print the result: [1, 2, 3, 4, '20140901']

Lst3 = [1, 2, 4]
Lst3.extend ([5, 6, 7, 8])
Print lst3 # print the result: [1, 2, 3, 4, 5, 6, 7, 8]

Lst4 = [1, 2, 4]
Lst4.extend ("5678 ")
Print lst4 # print the result: [1, 2, 3, 4, '5', '6', '7', '8']

Lst5 = []
For I in range (10 ):
Lst5 + = [I]
Print lst5 # print the result: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Lst6 = []
For I in range (10): # print the result: ['A', 'B', 'A', 'B', 'A', 'A', 'B ', 'A', 'B', 'A', 'B', 'A', 'A', 'B', 'B', 'A', 'B ', 'A', 'B', 'A', 'B']
Lst6 + = 'AB'
Print lst6

 

 

However, you cannot directly use list + string.

 

 

 lst1 = []
print lst1
lst2 = lst1 + '123456' #TypeError: can only concatenate list (not "str") to list
print lst2

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.