• day7(2)


    day 7 索引切片
     
     
    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 表示个数
     
     
    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
     
     
    返回找到的元素的索引,如果找不到报错
     
  • 相关阅读:
    48. Rotate Image
    47. Permutations II
    46. Permutations
    45. Jump Game II
    44. Wildcard Matching
    43. Multiply Strings
    42. Trapping Rain Water
    Python_匿名函数
    Python_内置函数之map()
    Python_面向对象_单例模式
  • 原文地址:https://www.cnblogs.com/wyfmc/p/11722070.html
Copyright © 2020-2023  润新知