zoukankan      html  css  js  c++  java
  • 40-字符串类型内置方法

    字符串类型内置方法

    一.字符串类型的概念

    1.1 用途

    描述性质的东西,如人的名字,单个爱好,地址,国家等。

    1.2定义

    使用:' '," ",""" """等包裹的一串串字符

    • u'unicode':Unicode编码的字符串
    • b'101':二进制编码的字符串
    • r' ':原生字符串,也就是说 '这是简单的两个字符串,并没有换行的意思
    s1 = str(520)
    s2 = str([1314])
    
    print(f's1:{s1}')
    print(f'type:{type(s1)}')
    
    print(f's2:{s2}')
    print(f'type:{type(s2)}')
    
    # s1:520
    # type:<class 'str'>
    # s2:[1314]
    # type:<class 'str'>
    

    二.常用操作及内置方法

    2.1 基本操作

    2.1.1 按索引取值(值取值不改变)

    # str索引取值
    msg = 'hello lwx'
    #      0123456789  # 索引序号
    
    print(f'索引为6: {msg[6]}')
    print(f'索引为-3: {msg[-3]}')
    
    # 索引为6: l
    # 索引为-3: l
    

    2.1.2 切片(顾头不顾尾,步长)

    print(f'切片3-最后: {msg[3:]}')
    print(f'切片3-8: {msg[3:8]}')
    print(f'切片3-8,步长为2: {msg[3:8:2]}')
    print(f'切片3-最后,步长为2: {msg[3::2]}')
    
    # 了解,步长为正从左到右;步长为负从右到左
    print('
    **了解知识点**')
    print(f'切片所有: {msg[:]}')
    print(f'反转所有: {msg[::-1]}')
    print(f'切片-5--2: {msg[-5:-2:1]}')
    print(f'切片-2--5: {msg[-2:-5:-1]}')
    
    
    # 切片3-最后: lo lwx
    # 切片3-8: lo lw
    # 切片3-8,步长为2: l w
    # 切片3-最后,步长为2: l w
    # 
    # **了解知识点**
    # 切片所有: hello lwx
    # 反转所有: xwl olleh
    # 切片-5--2: o l
    # 切片-2--5: wl
    

    2.1.3 长度(len)

    # str长度
    msg = 'hello lwx'
    
    print(len(msg))
    
    #9
    

    2.1.4 成员运算in和not in

    # str成员运算
    msg = 'my name is lwx, lwx handsome'
    
    print(f"'lwx' in msg: {'nick' in msg}")
    print(f"'jason' not in msg: {'jason' not in msg}")
    print(f"not 'jason' in msg: {not 'jason' in msg}")
    
    # 'lwx' in msg: False
    # 'jason' not in msg: True
    # not 'jason' in msg: True
    

    2.1.5 移除空白strip()

    # str移除空白strip()
    name = '&&&l wx'
    
    print(f"name.strip('&'): {name.strip('&')}")  # strip()默认为‘ ’,并且不修改原值,新创建空间
    print(f"name: {name}")
    
    # strip()应用场景
    pwd = input('password: ')  # 用户可能会手抖输入空格
    if pwd.strip() == '123':
        print('密码输入成功')
    
    print(f"'*-& lwx+'.strip('*-& +'): {'*-& lwx+'.strip('*-& +')}")
    
    
    name.strip('&'): l wx
    name: &&&l wx
    password: 123
    密码输入成功
    '*-& lwx+'.strip('*-& +'): lwx
    

    2.1.6 切分 split

    info = 'lwx:male:19'
    info_list1 = info.split(':')
    info_list2 = info.split(':', 1)
    
    print(f'info_list1:{info_list1}')
    print(f'info_list2:{info_list2}')
    
    
    # info_list1:['lwx', 'male', '19']
    # info_list2:['lwx', 'male:19']
    

    2.1.7 循环

    msg = 'hello lwx'
    
    for i in msg:
        print(i)
    
    # h
    # e
    # l
    # l
    # o
    # 
    # l
    # w
    # x
    

    2.2 需要掌握

    1.lstrip()和rstrip()

    # str之lstrip()和rstrip()
    name = '&&lwx&&'
    
    print(f"lwx.lstrip('&'): {name.lstrip('&')}")
    print(f"lwx.rstrip('&'): {name.rstrip('&')}")
    
    # lwx.lstrip('&'): lwx&&
    # lwx.rstrip('&'): &&lwx
    
    
    

    2.lower()和upper()

    # str之lower()和upper()
    name = 'lwx hanye'
    
    print(f"name.upper(): {name.lower()}")
    print(f"name.upper(): {name.upper()}")
    
    
    # name.upper(): lwx hanye
    # # # name.upper(): LWX HANYE
    
    

    3.startswith()和endswith()

    # str之startswith()和endswith()
    name = 'lwx hanye'
    
    print(f"name.startswith('lwx'): {name.startswith('lwx')}")
    print(f"name.endswith('chen'): {name.endswith('chen')}")
    
    # name.startswith('lwx'): True
    # name.endswith('chen'): False
    
    

    4.rsplit()

    # str之rsplit()
    info = 'lwx:male:19'
    
    print(f"info.rsplit(':', 1): {info.rsplit(':', 1)}")  # 从右开始切割
    
    # info.rsplit(':', 1): ['lwx:male', '19']
    
    

    5.join()

    # lis = [1,2,'19']
    # print(f"':'.join(lis): {':'.join(lis)}")  # 报错,数字不可和字符串拼接
    # str之join()
    lis = ['lwx', 'male', '19']
    
    print(f"':'.join(lis): {':'.join(lis)}")
    
    # ':'.join(lis): lwx:male:19
    
    

    6.replace()

    # str值replace()
    name = 'lwx shuai'
    
    print(f"name.replace('shuai','handsome'): {name.replace('shuai','handsome')}")
    
    name.replace('shuai','handsome'): lwx handsome
    
    

    7.isdigit()

    # str值isdigit()
    salary = '111'
    print(salary.isdigit())  # True
    
    salary = '111.1'
    print(salary.isdigit())  # False
    
    # True
    # False
    
    
    # str之isdigit()应用场景
    age = input('age: ')
    if age.isdigit():
        age = int(age)
    
        if age < 18:
            print('小姐姐')
        else:
            print('阿姨好')
    else:
        print(f'你的年龄能是这个{age}?')
    
    # age: 16
    # 小姐姐
    
    

    2.3其他操作

    2.3.1 find(),rfind(),index(),rindex(),count()

    # str之find()、rfind()、index()、rindex()、count()
    msg = 'my name is zhuzhu, zhuzhu shi sb, hha'
    
    print(f"msg.find('zhuzhu'): {msg.find('zhuzhu')}")  # 找不到返回-1
    print(f"msg.find('zhuzhu',0,3): {msg.find('zhuzhu',0,3)}")
    print(f"msg.rfind('zhuzhu'): {msg.rfind('zhuzhu')}")  # 找不到返回-1
    print(f"msg.index('zhuzhu'): {msg.index('zhuzhu')}")  # 找不到报错
    print(f"msg.rindex('zhuzhu'): {msg.rindex('zhuzhu')}")  # 找不到报错
    
    print(f"msg.count('zhuzhu'): {msg.count('zhuzhu')}")
    
    # msg.find('zhuzhu'): 11
    # msg.find('zhuzhu',0,3): -1
    # msg.rfind('zhuzhu'): 19
    # msg.index('zhuzhu'): 11
    # msg.rindex('zhuzhu'): 19
    # msg.count('zhuzhu'): 2
    
    

    2.3.2 center ()just()rjust()zfill()

    # str之center()、ljust()、rjust()、zfill()
    print(f"'info lwx'.center(50,'*'): {'info lwx'.center(50,'*')}")
    print(f"'info lwx'.ljust(50,'*'): {'info lwx'.ljust(50,'*')}")
    print(f"'info lwx'.rjust(50,'*'): {'info lwx'.rjust(50,'*')}")
    print(f"'info lwx'.zfill(50): {'info lwx'.zfill(50)}")  # 默认用0填充
    
    # 'info lwx'.center(50,'*'): *********************info lwx*********************
    # 'info lwx'.ljust(50,'*'): info lwx******************************************
    # 'info lwx'.rjust(50,'*'): ******************************************info lwx
    # 'info lwx'.zfill(50): 000000000000000000000000000000000000000000info lwx
    
    

    2.3.3 expandtabs

    # str之expandtabs()
    print(f"a\tb\tc: %s"%('a	b	c	'))  # 默认制表符8个空格
    print(f"'a\tb\tc'.expandtabs(32): %s"%('a	b	c	'.expandtabs(32)))
    
    # a	b	c: a	b	c	
    # 'a	b	c'.expandtabs(32): a     
    
    

    2.3.4 captalize(),swapcase(),title()

    # str之captalize()、swapcase()、title()
    name = 'lwx handsome sWAPCASE'
    
    print(f"name.capitalize(): {name.capitalize()}")
    print(f"name.swapcase(): {name.swapcase()}")  # 大小写互转
    print(f"name.title(): {name.title()}")
    
    
    # name.capitalize(): Lwx handsome swapcase
    # name.swapcase(): LWX HANDSOME Swapcase
    # name.title(): Lwx Handsome Swapcase
    
    
    

    2.3.5 is系列

    • isdecimal(): 检查字符串是否值包含十进制字符,如果是返回True,否则返回False。
    • isdigit(): 如果字符串只包含数字则返回True,否则返回False。
    • isnumeric(): 如果字符串中只包含数字字符,则返回True,否则返回False。
    num = "1"  #unicode
    num.isdigit()   # True
    num.isdecimal() # True
    num.isnumeric() # True
    
    num = "1" # 全角
    num.isdigit()   # True
    num.isdecimal() # True
    num.isnumeric() # True
    
    num = b"1" # byte
    num.isdigit()   # True
    num.isdecimal() # AttributeError 'bytes' object has no attribute 'isdecimal'
    num.isnumeric() # AttributeError 'bytes' object has no attribute 'isnumeric'
    
    num = "IV" # 罗马数字
    num.isdigit()   # True
    num.isdecimal() # False
    num.isnumeric() # True
    
    num = "四" # 汉字
    num.isdigit()   # False
    num.isdecimal() # False
    num.isnumeric() # True
    
    ===================
    isdigit()
    True: Unicode数字,byte数字(单字节),全角数字(双字节),罗马数字
    False: 汉字数字
    Error: 无
    
    isdecimal()
    True: Unicode数字,全角数字(双字节)
    False: 罗马数字,汉字数字
    Error: byte数字(单字节)
    
    isnumeric()
    True: Unicode数字,全角数字(双字节),罗马数字,汉字数字
    False: 无
    Error: byte数字(单字节)
    
    ================
    import unicodedata
    
    unicodedata.digit("2")   # 2
    unicodedata.decimal("2") # 2
    unicodedata.numeric("2") # 2.0
    
    unicodedata.digit("2")   # 2
    unicodedata.decimal("2") # 2
    unicodedata.numeric("2") # 2.0
    
    unicodedata.digit(b"3")   # TypeError: must be str, not bytes
    unicodedata.decimal(b"3") # TypeError: must be str, not bytes
    unicodedata.numeric(b"3") # TypeError: must be str, not bytes
    
    unicodedata.digit("Ⅷ")   # ValueError: not a digit
    unicodedata.decimal("Ⅷ") # ValueError: not a decimal
    unicodedata.numeric("Ⅷ") # 8.0
    
    unicodedata.digit("四")   # ValueError: not a digit
    unicodedata.decimal("四") # ValueError: not a decimal
    unicodedata.numeric("四") # 4.0
    
    #"〇","零","一","壱","二","弐","三","参","四","五","六","七","八","九","十","廿","卅","卌","百","千","万","万","亿"
    
    

    2.3.6.is其他

    • isalnum(): 如果字符串至少有一个字符并且所有字符都是字母或数字则返回True,否则返回False。
    • isalpha(): 如果字符串至少有一个字符并且所有字符都是字母则返回True,否则返回False。
    • islower(): 如果字符串中只包含至少一个区分大小写的字符,并且所有这些(区分大小写的)字符都是小写,则返回True,否则返回False。
    • isspace(): 如果字符串中只包含空白,则返回True,否则返回False
    • isupper(): 如果字符串中包含至少一个区分大小写的字符,并且所有这些(区分大小写的)字符都是大写,则返回True,否则返回False。
    • istitle(): 如果字符串是标题类型的(见title()),则返回True,否则返回False。

    2.4 存一个值or多个值

    一个值

    2.5 有序or无序

    只要是有索引的,都是有序的,因此字符串是有序的。

    name = 'lwx'
    print(f'first:{id(name)}')
    name = 'lwx handsome'
    print(f'second:{id(name)}')
    
    # first:48487616
    # second:48522648
    

    2.6 可变or不可变

    不可变数据类型

  • 相关阅读:
    程序员需要的各种PDF格式电子书【附网盘免费下载资源地址】
    Web安全大揭秘
    tar 压缩解压命令详解
    django开发项目的部署nginx
    CentOS7安装mysql-python模块
    我的博客站点上线了
    2006
    centos7安装pip
    mysql删除匿名用户
    FilenameFilter 文件名过滤
  • 原文地址:https://www.cnblogs.com/LWX-YEER/p/11379863.html
Copyright © 2011-2022 走看看