﻿ 跟老齐学Python之list和str比较 - 军军小站|张军博客 >>welcome_str[0]'W'>>>welcome_str[1]'e'>>>welcome_str[len(welcome_str)-1]'u'>>>welcome_str[:4]'Welc'>>>" />

# 跟老齐学Python之list和str比较

```
>>> welcome_str = "Welcome you"
>>> welcome_str[0]
'W'
>>> welcome_str[1]
'e'
>>> welcome_str[len(welcome_str)-1]
'u'
>>> welcome_str[:4]
'Welc'
>>> a = "python"
>>> a*3
'pythonpythonpython'

>>> git_list = ["qiwsir","github","io"]
>>> git_list[0]
'qiwsir'
>>> git_list[len(git_list)-1]
'io'
>>> git_list[0:2]
['qiwsir', 'github']
>>> b = ['qiwsir']
>>> b*7
['qiwsir', 'qiwsir', 'qiwsir', 'qiwsir', 'qiwsir', 'qiwsir', 'qiwsir']

```

```
>>> first = "hello,world"
>>> welcome_str
'Welcome you'
>>> first+","+welcome_str  #用+号连接str
'hello,world,Welcome you'
>>> welcome_str       #原来的str没有受到影响，即上面的+号连接后从新生成了一个字符串
'Welcome you'
>>> first
'hello,world'

>>> language = ['python']
>>> git_list
['qiwsir', 'github', 'io']
>>> language + git_list   #用+号连接list，得到一个新的list
['python', 'qiwsir', 'github', 'io']
>>> git_list
['qiwsir', 'github', 'io']
>>> language
['python']

>>> len(welcome_str)  #得到字符数
11
>>> len(git_list)    #得到元素数
3

```

list和str的最大区别是：list是原处可以改变的，str则原处不可变。这个怎么理解呢？

```
>>> git_list
['qiwsir', 'github', 'io']

>>> git_list.append("python")
>>> git_list
['qiwsir', 'github', 'io', 'python']

>>> git_list[1]
'github'
>>> git_list[1] = 'github.com'
>>> git_list
['qiwsir', 'github.com', 'io', 'python']

>>> git_list.insert(1,"algorithm")
>>> git_list
['qiwsir', 'algorithm', 'github.com', 'io', 'python']

>>> git_list.pop()
'python'

>>> del git_list[1]
>>> git_list
['qiwsir', 'github.com', 'io']

```

```
>>> welcome_str
'Welcome you'

>>> welcome_str[1] = 'E'
Traceback (most recent call last):
File "

", line 1, in

TypeError: 'str' object does not support item assignment

>>> del welcome_str[1]
Traceback (most recent call last):
File "

", line 1, in

TypeError: 'str' object doesn't support item deletion

>>> welcome_str.append("E")
Traceback (most recent call last):
File "

", line 1, in

AttributeError: 'str' object has no attribute 'append'

```

```
>>> welcome_str
'Welcome you'
>>> welcome_str[0] + "E" + welcome_str[2:] #从新生成一个str
'WElcome you'
>>> welcome_str             #对原来的没有任何影响
'Welcome you'

```

```
>>> matrix = [[1,2,3],[4,5,6],[7,8,9]]
>>> matrix = [[1,2,3],[4,5,6],[7,8,9]]
>>> matrix[0][1]
2
>>> mult = [[1,2,3],['a','b','c'],'d','e']
>>> mult
[[1, 2, 3], ['a', 'b', 'c'], 'd', 'e']
>>> mult[1][1]
'b'
>>> mult[2]
'd'

```

list和str转化

str.split()

>>>help(str.split)

```
split(...)
S.split([sep [,maxsplit]]) -> list of strings
Return a list of the words in the string S, using sep as the delimiter string. If maxsplit is given, at most maxsplit splits are done. If sep is not specified or is None, any whitespace string is a separator and empty strings are removed from the result.

```

```
>>> line = "Hello.I am qiwsir.Welcome you."

>>> line.split(".")   #以英文的句点为分隔符，得到list
['Hello', 'I am qiwsir', 'Welcome you', '']

>>> line.split(".",1)  #这个1,就是表达了上文中的：If maxsplit is given, at most maxsplit splits are done.
['Hello', 'I am qiwsir.Welcome you.']

>>> name = "Albert Ainstain"  #也有可能用空格来做为分隔符
>>> name.split(" ")
['Albert', 'Ainstain']
"[sep]".join(list)

```

join可以说是split的逆运算，举例：

```
>>> name
['Albert', 'Ainstain']
>>> "".join(name)    #将list中的元素连接起来，但是没有连接符，表示一个一个紧邻着
'AlbertAinstain'
>>> ".".join(name)   #以英文的句点做为连接分隔符
'Albert.Ainstain'
>>> " ".join(name)   #以空格做为连接的分隔符
'Albert Ainstain'

```

QQ号联系： 360901061

【本文对您有帮助就好】