• 字符串内置方法


    字符串内置方法

    使用方法

    1.索引

    s = 'hello'
    print(s[1])
    

    e

    2.索引切片

    s='hello how are you'
    print(s[0:4])#顾头不顾尾
    print(s[0:4:2])#2表示步长,隔一个取一个
    print(1,s[4:0:-1])# +从左到右,-表示从右到左
    print(1,s[2:])# 左边的不写取到最左边,右边的不写取到最右边
    

    hell
    hl
    1 olle
    1 llo how are you

    3.for循环

    for i in s:
        print(4,i)
    

    4 h
    4 e
    4 l
    4 l
    4 o
    4
    4 h
    4 o
    4 w
    4
    4 a
    4 r
    4 e
    4
    4 y
    4 o
    4 u

    4.成员运算

    s='hello how are you'
    print('hello' in s)
    print('hello1' not in s)
    

    True
    True

    5.strip()

    默认除去两端空格,可以指定去除的字符,可以指定多个字符同时去掉

    s1 = '   juary     ***'
    print(s1.strip(' j*'))
    

    uary

    6.split() 切割

    s1 = 'hello|how|are|you'
    print(s1.split('|'))# 按照|切割字符串,得到的是一个列表
    
    

    ['hello', 'how', 'are', 'you']

    7. len 长度

    s='hello how are you'
    print(len(s))
    

    17

    8. lstrip()&rstrip()

    s2='**hello**'
    print(s2.lstrip('*'))#去掉左边的'*'
    print(s2.rstrip('*'))#去掉右边的'*'
    
    

    hello**
    **hello

    9. lower&upper

    s3='xiaomei'
    print(s3.lower())#小写
    print(s3.upper())#大写
    

    xiaomei
    XIAOMEI

    10. startswith&endswith

    s='hello how are you'
    print(s.startswith('hell'))#以。。。开始
    print(s.endswith('you'))#以。。。结束,如果是则返回True,否则返回False
    

    True
    True

    11. rsplit

    s1 = 'hello|how|are|you'
    print(s1.split('|',1))#从左切割
    print(s1.rsplit('|',1))#从右切割
    

    ['hello', 'how|are|you']
    ['hello|how|are', 'you']

    12. join

    Python join() 方法用于将序列中的元素以指定的字符连接生成一个新的字符串。

    s1 = 'hello|how|are|you'
    lt = s1.split('|')
    print(lt)
    print('*'.join(lt))#使用*拼接列表内的每一个元素
    

    ['hello', 'how', 'are', 'you']
    hello*how*are*you

    13. replace

    s='hello how are you'
    s=s.replace('hello','hai')
    print(s)
    

    hai how are you

    14. isdight&isalpha

    s='123456s'
    print(s.isdigit())#判断字符串内字符是否都为数字,返回bool值
    print(s.isalpha())#判断字符串内字符是否都为字母,返回bool值
    

    False
    False

    15. find rfind index rindex count

    s='hello how are you'
    print(s.find('h'))#查找索引,没有找到返回-1
    print(s.rfind('h',4,10))#(print(s,rfind))#(4,10)为在索引从(4,10)索引内查找
    
    s5 = 'aaasssaaa'
    print(s5.count('a'))#统计a的个数
    
    

    0
    6
    6

    16. center ljust rjust zfill

    s = 'hai'
    print(s.center(50,'*'))#居中
    print(s.ljust(50,'*'))#居左
    print(s.rjust(50,'*'))#居右
    print(s.zfill(9))#往字符串内左侧填充0,填充到一共9个字符
    
    

    hai*
    hai***********************************************
    ***********************************************hai
    000000hai

    换行 缩进 expandtabs把字符串中的 tab 符号(' ')转为空格,tab 符号(' ')默认的空格数是 8

    S = "this is	string example....wow!!!"
     
    print ("原始字符串: " + S)
    print ("替换 \t 符号: " +  S.expandtabs())
    print ("使用16个空格替换 \t 符号: " +  S.expandtabs(16))
    

    原始字符串: this is string example....wow!!!
    替换 符号: this is string example....wow!!!
    使用16个空格替换 符号: this is string example....wow!!!

    17. captalize swapcase title

    s='hello how are you'
    print(s.capitalize())#首字母大写
    print(s.swapcase())#所有字母小写变大写,大写变小写
    print(s.title())#每个单词的首字母大写
    

    Hello how are you
    HELLO HOW ARE YOU
    Hello How Are You

    ​ 有序 or 无序:有序(有索引则有序)
    ​ 可变 or 不可变:不可变(值变id不变为可变,值变id也变为不可变)

  • 相关阅读:
    mysql
    MySQL主从同步
    python与各数据库的交互
    snmptrap
    web场景的监控
    zabbix的历史数据存储到elasticsearch中
    使用PopupWindow的实现步骤
    使用PopupWindow的实现步骤
    ListView及其ArrayAdapter的应用
    ListView及其ArrayAdapter的应用
  • 原文地址:https://www.cnblogs.com/ghylpb/p/11514687.html
Copyright © 2020-2023  润新知