字符串分割:
split翻译为分裂。 split()就是将一个字符串分裂成多个字符串组成的列表。
split()当不带参数时以空格进行分割,当代参数时,以该参数进行分割。
例:
str='12%34%56%78'
print(str.split())
print(str.split('%'))
print(str.split('%',1))
['12%34%56%78']
['12', '34', '56', '78']
['12', '34%56%78']
1.理解索引这个会在之后经常用到
2.定义字符串、例如:str1 = 'http://www.jianshu.com/u/a987b338c373'字符串内容为自己的首页连接,输出自己的简书id(u/之后的内容--a987b338c373)
索引:
str1 = '[http://www.jianshu.com/u/d1b0f55d8efb]
print(str1[26:38])
分割:
['[http:', '', 'www.jianshu.com', 'u', 'd1b0f55d8efb]']
print(str1.split('/',4)[4])
d1b0f55d8efb
3.设s = "abcdefg", 则下列值为
s[3] = d s[2:4] = cd
s[:5] = abcde s[3:] = defg
s[::-1] = gfedcba s[::2] = aceg
s = "abcdefg"
print(s[3])
print(s[2:4])
print(s[:5])
print(s[3:])
print(s[::-1])
print(s[::2])
4.定义列表:list1 = [1,2,3,4,5,6,7],则下列值为
list1[3] = 4 list1[2:4] = [3,4]
list1[:5] = [1,2,3,4,5 ] list1[3:] = [4,5,6,7]
list1[::-1] =[ 7,6,5,4,3,2,1] list1[::2] = [1357]
5.定义元组:touple1 = (1,2,3,4,5,6,7),则下列值为
touple1[3] = 4 touple1[2:4] = (3,4)
touple1[:5] = (1,2,3,4,5) touple1[3:] = (4,5,6,7)
touple1[::-1] = (7,6,5,4,3,2,1) touple1[::2] = (1,3,5,7)
6.对之前学习过得集中基本类型及其方法进行复习,重中之重理解索引和切片