1,索引

     取单个值

  正向索引  0 1 2 3.........

   a="abcde"

   print(a[2]) 

  反向索引

  -1 -2 -3 -4 -5

 

步长:

 

 

 切片:取多个值

从左到右取值:

   原则:顾头不顾尾

   1,a[0:3]    abc   正向索引

   2,a[-5:-2]   abc   反向索引

   3,a[0:-2]    abc   正反向索引混合

从左到右取值:

原则:顾头不顾尾

1,a[2::-1]    cba   正向索引

2,a[-3::-1]  cbaa    反向索引

3,a[2:-6:-1]  cba  a[2:-5:-1]   cb    正反向索引混合

字符串的方法:

s = "abC"

upper,lower

print(s.upper())     实现字符串全部大写

print(s.lower())      实现字符串全部小写

replace    替换

    a = "abacad"

  print(a.replace("a","中国'))

  print(a.replace("a","中国",2))  2表示个数

capitalize

  首字母大写,其余字母小写

swapcase,#大小写翻转

strip

   去掉开头和结尾的空格,特定的字符

  print(a.strip())
      a="&&a&bc&&"

     print(a.strip("&"))

startswith,endswith

结果是bool值,支持切片

     print(s.startswith("a"))    判断一。。。开头

     print(s.endswith("a"))      判断以。。。结尾

     print(s.startswith("a",1,4))   切片部分判断

公共方法:

    count()

         s="abac"

         print(s.count("a"))     #a元素出现的次数

len()    print(len(s))     对象的长度

 

split   str---->list

   str分割为列表,默认以空格分割

     s.split()   默认空格

     s.split("符号")

 

 

 join  list---->str

    列表里面元素用指定符号进行连接

   形式:"分隔符".join(list)

find

   返回找到的元素的索引,如果找不到返回-1

index

   返回找到的元素的索引,如果找不到报错

 

posted on 2019-10-30 20:52  王征  阅读(98)  评论(0)    收藏  举报