• Python(七)——字符串的详细使用


    Python(七)——字符串和列表的详细使用

    字符串拼接

    方式一:将两个字符串常量紧挨在一起,打印时会拼接起来

    str3 = 'Process finished ''with exit code'
    print(str3)
    
    • 1
    • 2

    执行结果:这种方式只适用于字符串常量的拼接

    Process finished with exit code
    
    • 1

    方式二:使用+将两个字符串拼接

    str1 = 'Process finished '
    str2 = 'with exit code'
    print(str1+str2)
    str3 = 'Process finished '+'with exit code'
    print(str3)
    
    • 1
    • 2
    • 3
    • 4
    • 5

    执行结果:这种方式可以拼接字符串常量和变量

    Process finished with exit code
    Process finished with exit code
    
    • 1
    • 2

    方式三:join()方法,join()会将列表(或元组)中多个字符串使用指定的分隔符拼接在一起

    my_list = ['www','weipc','top']
    print('.'.join(my_list))
    str14 = 'x'
    print(str14.join("python"))
    
    • 1
    • 2
    • 3
    • 4

    执行结果:

    www.weipc.top
    pxyxtxhxoxn
    
    • 1
    • 2

    字符串和数字的拼接

    拼接字符串和数字,需要先把数字从数值类型转成字符串类型,可以使用str()方法

    name = "李逍遥"
    age = 18
    info = name + "已经" + str(age) + "岁了"
    print(info)
    
    • 1
    • 2
    • 3
    • 4

    执行结果:

    李逍遥已经18岁了
    
    • 1

    获取字符串长度 len()

    str4 = 'Process finished 过程完成'
    #获取字符串长度
    print(len(str4))
    #获取字符串采用UTF-8编码时的字节数
    print((len(str4.encode())))
    #获取字符串采用GBK编码时的字节数
    print((len(str4.encode('gbk'))))
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    执行结果:

    21
    29
    25
    
    • 1
    • 2
    • 3

    查找内容 find()

    用于检索指定内容在字符串中是否存在,如果存在就返回该内容在字符串中第一次出现的索引值,反之则返回-1

    str5 = 'Process'
    print(str5.find('ess'))
    print(str5.find('a'))
    
    • 1
    • 2
    • 3

    执行结果:

    4
    -1
    
    • 1
    • 2

    startswith() 和endswith()

    startswith() 方法用于判断字符串是否以指定字符串开头,是返回 True;反之返回 False。

    endswith() 方法用于判断字符串是否以指定字符串结尾,是返回 True;反之返回 False。

    str6 = 'Process'
    print(str6.startswith('P'))
    #从字符串指定位置开始判断
    print(str6.startswith('c',3))
    print(str6.endswith('s'))
    print(str6.endswith('t'))
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    执行结果:

    True
    True
    True
    False
    
    • 1
    • 2
    • 3
    • 4

    计算出现次数 count()

    count 方法用于返回指定字符串在另一字符串中出现的次数,不存在返回 0,存在就返回出现的次数。

    str7 = 'Process finished in the'
    str8 = 'in'
    str9 = 'abc'
    print(str7.count(str8))
    print(str7.count(str9))
    # 指定检索的起始位置
    print(str7.count(str8,10))
    # 指定检索的起始位置和终止位置
    print(str7.count(str8,12,20))
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    执行结果:

    2
    0
    1
    1
    
    • 1
    • 2
    • 3
    • 4

    替换字符串 replace()

    replace() 方法把字符串中的旧字符串替换成新字符串。

    str10 = 'Process finished'
    print(str10.replace('s','x'))
    #指定最大替换次数
    print(str10.replace('s','x',2))
    
    • 1
    • 2
    • 3
    • 4

    执行结果:

    Procexx finixhed
    Procexx finished
    
    • 1
    • 2

    分割字符串 split()

    split()方法将一个字符串按照指定的分隔符切分成多个子字符串,这些子字符串会被保存到列表中(不包含分隔符),作为方法的返回值反馈回来。

    str11 = 'Process finished with exit code'
    print(str11.split(' '))
    print(str11.split('i'))
    print(str11.split('i',2))
    
    • 1
    • 2
    • 3
    • 4

    执行结果:

    ['Process', 'finished', 'with', 'exit', 'code']
    ['Process f', 'n', 'shed w', 'th ex', 't code']
    ['Process f', 'n', 'shed with exit code']
    
    • 1
    • 2
    • 3

    大小写转换 title()、lower() 和 upper()

    title():只将首字母转换成大写,其他转换成小写

    lower():将字符串转换成大写

    upper():将字符串转换成小写

    str12 = 'Process finished With EXIT code'
    print(str12.title())
    print(str12.upper())
    print(str12.lower())
    
    • 1
    • 2
    • 3
    • 4

    执行结果:

    Process Finished With Exit Code
    PROCESS FINISHED WITH EXIT CODE
    process finished with exit code
    
    • 1
    • 2
    • 3

    去除字符串中的空格和特殊字符 strip(),lstrip(),rstrip()

    strip(),去除字符串左右两侧的空格或特殊字符

    lstrip(),去除字符串左侧的空格或特殊字符

    rstrip(),去除字符串右侧的空格或特殊字符

    特殊字符指制表符(\t)、回车符(\r)、换行符(\n)等。

    str13 = ' Process finished with exit code '
    print(str13.strip())
    print(str13.lstrip())
    print(str13.rstrip())
    #移除字符串头尾指定的字符序列
    print(str13.strip('e '))
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    执行结果:返回空格被删除之后的字符串副本,并不会改变原字符串

    Process finished with exit code
    Process finished with exit code 
     Process finished with exit code
    Process finished with exit cod
    
    • 1
    • 2
    • 3
    • 4
  • 相关阅读:
    z—libirary最新地址获取,zlibirary地址获取方式,zliabary最新地址,zliabary官网登录方式,zliabary最新登陆
    Restful风格的编程
    【C#】XML的基础知识以及读取XML文件
    FlutterAcivity 包已导入 但是仍然爆红
    ESP32烧写Arduino编译的固件
    美团前端vue面试题(边面边更)
    python绘图技巧(高清图)
    基于Jenkins的CI/CD实践(kaniko版本)
    常用vim操作
    积分值和面积、对称性
  • 原文地址:https://blog.csdn.net/wpc2018/article/details/125612759