1.列表以[]定义,字符串用单引号或双引号标识,数字不用引号,都用逗号分隔。
2.字符串本身也可以是一个数组。
3.正数索引从0开始,一次从左往右;负数索引从-1开始,从右往左。
4.整型和字符串不能直接拼接(详情见示例2)
示例1:
1 #!/usr/bin/env python 2 # _*_ coding:utf-8 _*_ 3 family = ["Mom","Dad","sister","brother"] 4 name = ['z','y','t'] 5 num =[1,2,3,4] 6 str="hello" 7 print(family[1]) 8 print(family[-1]) 9 print(str[1])
输出结果:
1 Dad 2 brother 3 e
示例2:
1 #列表拼接 2 #列表 3 print([1,2,3]+[4,5,6]) 4 print("hello "+"word!") 5 print([1,2,3] + ["hello"]) 6 #整型不可以和字符串直接拼接 7 #print([1,2,3] + "hello") #报错 8 #print(2 + "hello") #报错
输出结果
[1, 2, 3, 4, 5, 6] hello word! [1, 2, 3, 'hello']