切片,字符串

1,索引

    取的是单个值

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

   a="abcde"

   print(a[2])         #c

反向索引

-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]   cba   反向索引

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表示个数

captalize,

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

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))   切片部分判断

公共方法:

   cont()

         s="abac"

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

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

 

split      str--->list

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

      s.split()   默认空格

      s.split("符号")

      

join    list--->str

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

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

   

 

 

posted @ 2019-10-22 17:14  待会聊  阅读(145)  评论(0)    收藏  举报