python索引

索引切片

1    索引

     取的是单个值

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

      a = "abce"

      print(a[2])      #c

反向索引

  -1   -2   -3  -4  -5

   

 步长

      

   切片:  取多个值

  从左到右取值:

      原则: 顾头不顾尾

1    a[2::-1]      cba

2     a[-3::-1]    cba

3      a[2:-6:-6}     cba    a [2:5:-1}   cb

字符串的方法:

 s =  "abc"

upper,lower

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

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

   replace    替换

     a   =   "abcad"

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

  公共方法

   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)

     

     

原文地址:https://www.cnblogs.com/lisen321/p/11720509.html