• 字符串


    数字类型的计算

    ① - int() #将字符串转换为数字

    a = "123" #赋值a为字符串123
    print(type(a),a) #输出a的数据类型和a
    b = int(a) #将a字符串中的数字转换为数字类型并赋值于b print(type(b),b) #输出b的数据类型和b

    num = "0011" #将0011赋值于num
    v = int(num, base=16) #将16进制的num转换为10进制的v
    print(v) #输出v

    ② - bit_lenght()  #当前数字的二进制,至少用n位表示

    r = age.bit_length()

    5、字符串类型的计算

    ① .capitalize() #字符串首字符大写

    a = 'alex'
    b = a.capitalize()
    print(b)

    输出结果:Alex

    ② .casefold() #变小写,很多未知的对相应变小写,与lower类似但比lower跟强大

    a = 'ALEX'
    b = a.casefold()
    print(b)

    输出结果:alex 

    ③ .center() #将字符串放在中间,在指定长度下,首尾以指定字符填充

    a = 'Title'
    b = a.center(20,'*') #总长度为20,前后字符以*填充
    print(b)

    输出结果:*******Title********

    ④ .count() #计算字符串中某字符的出现数量

    a = 'Titleleitttt'
    b = a.count('le',5,8) #在第5-8位中寻找字符le的数量
    print(b)

    输出结果:1

    ⑤ .endswith() #判断是否以该字符结尾

      .startswith() #判断是否以该字符开始

    a = 'Titleleitttt'
    b = a.startswith('Ti') #判断是否以Ti开头
    c = a.endswith('te') #判断是否以te结尾
    print(b) 
    print(c)

    输出结果:

    True #正确
    False #错误

    ⑥.find() #在字符串中寻找指定字符的位置

    a = 'Titleleitttt'
    b = a.find('lei') #寻找字符lei的位置
    print(b)

    输出结果:5

    ⑦ .format() #字符格式化,将字符串中的占位符替换为指定的值

    a = 'i am {name} , age {num} ' 
    b = a.format(name = 'alex',num = 20) #将a中的name更改为alex,num更改为20 
    print(b)

    输出结果:i am alex , age 20 

    ⑧ .isalnum() #判断字符串中是否只包含字母和数字

    text1 = "123-"
    text2 = "123"
    b = text1.isalnum()
    c = text2.isalnum()
    print(b)
    print(c)

    输出结果:

    False
    True

    ⑨ .expandtabs()  #断句

    test = "jdoais	joijqwo	ihdwfj	oi" 
    v = test.expandtabs(6) #将字符串中的 tab 符号('	')转为6个空格数
    print(v)

    输出结果:jdoais      joijqwo     ihdwfj      oi

    ⑩ .index() #检测字符串中是否包含子字符串,并输出开始位置/没有时报错

    test = "alexader"
    v = test.index("der") 
    print(v)

    输出结果:5

    ⑪ .isalpha() #检测字符串是否只由字母组成

    test = "alexa1der"
    v = test.isalpha()
    print(v)

    输出结果:False

    ⑫ 三种检测数字的语法

    test = ""
    v1 = test.isdecimal() #检查字符串是否只包含十进制字符
    v2 = test.isdigit() #检测字符串是否只由数字组成,可检测②,不可检测出二
    v3 = test.isnumeric() #检测字符串是否只由数字组成,可检测②、二
    print(v1,v2,v3)

    输出结果:False True True

    ⑬ .isprintable() #检测字符串中是否存在不可显示的字符

    test1 = ""
    test2 = ""
    v1 = test1.isprintable()
    v2 = test2.isprintable()
    print(v1,v2)

    输出结果:True False

    ⑭ .isspace() #判断是否全部是空格

    test1 = " "
    test2 = "san  di"
    v1 = test1.isspace()
    v2 = test2.isspace()
    print(v1,v2)

    输出结果:True False

    ⑮标题格式

    .istitle() #检测字符串中所有的单词拼写首字母是否为大写,且其他字母为小写

    .title() #将字符串更改为标题格式

    test = "Return True if all cased characters in S are uppercase and there is"
    v1 = test.istitle() #检测test字符串是否为标题格式
    print(v1)
    v2 = test.title() #将test字符串更改为标题格式
    print(v2)
    v3 = v2.istitle() #检测v2字符串是否为标题格式
    print(v3)

    输出结果:

    False
    Return True If All Cased Characters In S Are Uppercase And There Is
    True

    ⑯ .join() #将序列中的元素以指定的字符连接生成一个新的字符串

    test = '大河向东流'
    print(test)
    v = '*'.join(test) #将*号加入到test字符串的各子字符串之间
    print(v)

    输出结果:

    大河向东流
    大*河*向*东*流

    ⑰小写

    .islower() #判断是否全部是小写

    .lower() #转换为小写

    .isupper #判断是否全部是大写

    .upper() #转换为大写

    test = 'CONSIDER'
    test2 = 'consider'
    v1 = test.islower() #判断test字符串是否为小写
    v2 = test.lower() #将test字符串转换为小写
    v3 = v2.islower() #判断V3字符串是否为小写
    n1 = test2.isupper()
    n2 = test2.upper()
    n3 = n2.isupper()
    print(v1,v2,v3)
    print(n1,n2,n3)

    输出结果:

    False consider True
    False CONSIDER True

    ⑱ 移除指定字符串

    .strip() #移除字符串头尾指定的字符(默认为空格)

    .lstrip() #用于截掉字符串左边的空格或指定字符

    .rstrip() #用于截掉字符串右边的空格或指定字符

    test = '0000this  is wonderful000'
    v1 = test.strip('00')
    v2 = test.lstrip('00')
    v3 = test.rstrip('00')
    print(v1)
    print(v2)
    print(v3)

    输出结果:

    this is wonderful
    this is wonderful000
    0000this is wonderful

    ⑲ str.maketrans() #对应关系转换

    a = 'abcde'
    b = '12345'
    test = 'jk12345edcba54321abcdekj'
    v = str.maketrans(a,b) #将a字符串转换成b字符串中的子字符
    new_v = test.translate(v) #将test中的字符串转换成v的关系
    print(new_v)

    输出结果:jk12345543215432112345kj

    ⑳ 分割

    .partition() #根据指定的分隔符将字符串进行分割

    .rpartition() #根据指定的分隔符将字符串从右边开始进行分割

    .split() #分隔符切片 string,如果 num有指定值,则仅分隔 num 个子字符串

    test = 'daishfojisjdadasfdsddd'
    p1 = test.partition('s')
    p2 = test.rpartition('s')
    s3 = test.split('s')
    s4 = test.split('s',3) #将test字符串以s为分隔符分割成3次
    print(p1)
    print(p2)
    print(s3)
    print(s4)

    输出结果:

    ('dai', 's', 'hfojisjdadasfdsddd')
    ('daishfojisjdadasfd', 's', 'ddd')
    ['dai', 'hfoji', 'jdada', 'fd', 'ddd']
    ['dai', 'hfoji', 'jdada', 'fdsddd']

    ㉑ .splitlines() #按照行(' ', ' ', ')分隔,返回一个包含各行作为元素的列表。参数选择true,false:是否保留换行

    test = 'daishf
    ojisjdad
    asfdsddd'
    v = test.splitlines()
    print(v)

    输出结果:['daishf', 'ojisjdad', 'asfdsddd']

    ㉒ .swapcase() #大小写转换 

    test = 'ComeOnD'
    v = test.swapcase()
    print(v)

    输出结果:cOMEoNd

    ㉓ .isidentifier() #判断字符串是否为字母、数字和下划线

    test1 = '|a2'
    test2 = '_a2'
    v1 = test1.isidentifier()
    v2 = test2.isidentifier()
    print(v1)
    print(v2)

    输出结果:

    False
    True

    ㉔ .replace() #把字符串中的 old(旧字符串) 替换成 new(新字符串),如果指定第三个参数max,则替换不超过 max 次

    test = 'daexdjiexdjiolkjexdddd'
    v1 = test.replace('ex','OK')
    v2 = test.replace('ex','NO2',2) #只能替换两次
    print(v1)
    print(v2)

    输出结果:

    daOKdjiOKdjiolkjOKdddd
    daNO2djiNO2djiolkjexdddd

  • 相关阅读:
    PostgreSQL configure: error: readline library not found
    JS实现的模态框弹窗并自动消失
    MySQL安装详细教程
    C++与C#中枚举的区别
    句柄
    RESTFul API
    四、使用ADB命令清除缓存
    C#枚举案例
    Pycharm安装教程
    七、Monkey实战
  • 原文地址:https://www.cnblogs.com/lishuangtu/p/8876173.html
Copyright © 2020-2023  润新知