zoukankan      html  css  js  c++  java
  • 字符串操作

    # 1.capitalize(cap ita lize)
    # 功能:字符串首字母大写
    # Ex1:
    # name = 'max'
    # print(name.capitalize())
    # 回车:Max

    # Ex2:
    # name = 'max alex bob pop'
    # A = name.capitalize() # print(name.capitalize())
    # print(A)

    # 2.casefold(case fold)
    # 功能:字符串字母小写
    # Ex:
    # name = 'MAX'
    # print(name.casefold())
    # 回车:max

    # 3.center(cen ter)
    # 功能:字符串居中
    # Ex:
    # name = 'Max'
    # A = name.center(9,'*')
    # print(A)
    # 回车:***Max***
    # 注:
    # center本身自带两个参数(1,"*")
    # (1,"*")前者代表字符串回车后总长度:后者表示左右两侧要添加的符号,可为空格

    # 4.count
    # 功能:统计某个字符在字符串中出现的次数,或在指定区间内完成上述操作
    # Ex1(整间):
    # name = 'leonardo wilelm dicaprio'
    # A = name.count('e')
    # print(A)
    # 回车:2

    # Ex2(区间):
    # name = 'leonardo wilelm dicaprio'
    # A = name.count('e',1,8)
    # print(A)
    # 回车:1
    # 注:(1,8)在python中对于范围讲究顾头不顾尾的原则

    # Ex3(大小写区分):
    # name = 'Leonardo Leonardo Leonardo'
    # A = name.count('l')
    # print(A)
    # 回车:0

    # name = 'Leonardo Leonardo Leonardo'
    # A = name.count('L')
    # print(A)
    # 回车:3

    # name = 'Leonardo Leonardo Leonardo'
    # A = name.casefold().count('l')
    # print(A)
    # 回车:3

    '''
    5.encode
    what the fuck???
    '''

    # 6.endswith(end swith)
    # 功能:判断字符串是否以某个字符串结尾的,返回值为bool
    # Ex1:
    # A = 'Cyberpunk None'
    # print(A.endswith('None'))
    # 回车:True

    # Ex2:
    # A = 'Cyberpunk None'
    # print(A.endswith('e'))
    # 回车:True

    # Ex3:
    # A = 'Cyberpunk None'
    # print(A.endswith('E'))
    # 回车:False

    '''What the fuck??
    # 7.expandtabs(ex pand tabs)
    # 功能:扩展标签
    Ex:
    li = 'sw ht'
    li.expandtabs()
    print(li)
    '''

    # 8.find
    # 功能:查找查找指定字符串序列坐标,找不到回-1
    # Ex1:
    # name = 'max'
    # print(name.find('a'))
    # 回车:1

    # Ex2:
    # name = 'max'
    # print(name.find('A'))
    # 回车:-1

    # Ex3:
    # name = 'max'
    # print(name.find('m'))
    # 回车:0

    # 9.format(for mat)
    # 功能:{ } { } 占位符(借鉴理解%s)
    # Ex1:
    # speech = '{} have a {}.'
    # print(speech.format('You','dog'))
    # 回车:You have a dog.

    # Ex2:
    # speech = '{} have a {}.'
    # print(speech.format('I','dream'))
    # 回车:I have a dream.

    # 10.__contains__(con tains)
    # 功能:包含,指定字符串是否被包含,返回bool
    # Ex1:
    # speech = 'I have a dream'
    # print(speech.__contains__('av'))
    # 回车:True

    # Ex2:
    # speech = 'I have a dream'
    # print(speech.__contains__('T h'))
    # 回车:False

    # 11.index
    # 功能:指数(在字符串中查找指定的字符串坐标,找不到时直接报错)
    # Ex1:
    # name = 'max'
    # print(name.index('x'))
    # 回车:2

    # Ex1:
    # name = 'max'
    # print(name.index('X'))
    # 回车:报错,substring not found (未找到子字符串)

    # 12.'*'join()
    # 功能:间接添加
    # Ex1:
    # name ='max'
    # print('@'.join(name))
    # 回车:m@a@x

    # Ex2:
    # name ='m a x'
    # print('**'.join(name))
    # 回车:m** **a** **x

    # 13.isalnum(is al num)
    # 功能:检查判断字符串是否包含字母数字字符,回车bool
    # Ex1:
    # ID = '85757max'
    # print(ID.isalnum())

    # Ex2:
    # ID = 'max'
    # print(ID.isalnum())

    # 14.isalpha(is al pha)alpha字母表
    # 功能:检测字符串是否包含字母组成,回车bool
    # Ex1.
    # name = 'max'
    # print(name.isalpha())
    # 回车:True

    # Ex2.
    # ID = '89757max'
    # print(ID.isalpha())
    # 回车:False

    # Ex3.
    # ID = '2.2'
    # print(ID.isalpha())
    # 回车:False

    # 15.lower
    # 功能:将所有字母转换成小写
    # Ex1.
    # name = 'Max'
    # print(name.lower())
    # 回车:max

    # 16.isdecimal(is decimal)
    # 功能:判断字符串是否为十进制,回车bool
    # Ex1.
    # ID = '101'
    # print(ID.isdecimal())
    # 回车:True

    # Ex2.
    # ID = 'Max101'
    # print(ID.isdecimal())
    # 回车:False

    # Ex3.
    # ID = '54.1'
    # print(ID.isdecimal())
    # 回车:False

    # 17.isdigit(is digit)
    # 功能:判断字符串是否仅为数字组成,回车bool
    # Ex1.
    # ID = '555'
    # print(ID.isdigit())
    # 回车:True

    # Ex2.
    # ID = 'MAX555'
    # print(ID.isdigit())
    # 回车:False

    # 18.isidentifier(isi dent ifi er)
    # 功能:判断字符串是否为字母开头,回车bool
    # Ex1.
    # name = 'MAX'.isidentifier()
    # print(name)
    # 回车:True

    # Ex2.
    # name = '0123max'
    # A = name.isidentifier()
    # print(A)
    # 回车:False

    # 19.isnumeric(is num eric)
    # 功能:判断字符串是否只由数字组成,和isdigit区别????,回车bool
    # Ex1.
    # ID = '222000'.isnumeric()
    # print(ID)
    # 回车'True'

    # Ex2.
    # ID = 'ABC 000'.isnumeric()
    # print(ID)
    # 回车:False

    # 20.isprintable (is print able)
    # 功能:判断字符串是否为能够打印的,回车bool
    # Ex1.
    # ID = " Max".isprintable()
    # print(ID)
    # 回车:False

    # Ex2.
    # ID = "Max".isprintable()
    # # print(ID)
    # 回车:True

    # 21.isspace(is space)
    # 功能:判断字符串是否仅为空格,回车bool
    # Ex1.
    # ID = ' '.isspace()
    # print(ID)
    # 回车:True

    # Ex2.
    # ID = ' Max'.isspace()
    # print(ID)
    # 回车:False

    # 22.istitle(is title)
    # 功能:判断字符串每节是否首字母大写,回车bool
    # Ex1.
    # name = 'Max'.istitle()
    # print(name)
    # 回车:True

    # Ex2.
    # name = 'Ace Max'.istitle()
    # print(name)
    # 回车:True

    # Ex3.
    # name = 'Ace max'.istitle()
    # print(name)
    # 回车:False

    # Ex4.
    # name = 'max'.capitalize().istitle()
    # print(name)
    # 回车:True

    # 23.isupper(is upper)
    # 功能:判断字符串是否都为大写字母,回车bool
    # Ex1.
    # name = 'Max'.isupper()
    # print(name)
    # 回车:False

    # Ex2.
    # name = 'MAX'.isupper()
    # print(name)
    # 回车:True

    # 24.ljust
    # 功能:左对齐 自带参数(步数,'填充内容')
    # Ex.
    # name = 'max'.ljust(30,'*')
    # print(name)
    # 回车:max***************************

    # 25.strip(rstrip 右去空 lstrip 左去空)
    # 功能:去空
    # Ex.用户不小心输入了 空格空格 + max + 空格空格空格空格空格
    # name = input('Please put your username>>:( MAX )').strip().lower().capitalize()
    # if name == 'Max':
    # print('happy birthday')
    # 回车:happy birthday

    # 注:假设输入的是空格 MAX 空格
    # strip().lower().capitalize()
    # 先将字符串去空,然后改为全部小写,然后将首字母大写
    # 空格 MAX 空格 : max : Max
    # strip 不能去除中间空格 M A X

    # 26.maketrans ????

    # 27.partition(part ition)
    # 功能:分区,分隔字符串
    # Ex.
    # name = 'MAXBOBALICE'.partition('BOB')
    # print(name)
    # 回车:('MAX', 'BOB', 'ALICE')

    # 28.replace(re place)
    # 功能:替换,代替,将字符串中需要替换的字符串替换
    # Ex.
    # speech = 'I have a dream.'.replace('I','You need')
    # print(speech)
    # 回车:You need have a dream.

    # 29.split
    # 功能:分隔,默认空格
    # Ex.
    # name = 'MaxWang'.split('w'.upper())
    # print(name)
    # 回车:['Max', 'ang']

    # 30._add_
    # 功能:在字符串结尾添加字符串
    # Ex.
    # name = 'Ma'.__add__('X'.lower())
    # print(name)
    # 回车:Max

    # 31._eq_
    # 功能:判断字符串是否相等
    # Ex.
    # A = '250'
    # B = '250'
    # print(A.__eq__('250'))
    # print(A.__eq__(B))
    # 回车:True
    # 回车:True
  • 相关阅读:
    VMware提示获取所有权利
    解决错误-bash: ./configure: Permission denied
    Linux ffmpeg 支持x264 If you really want to compile without asm, configure with --disable-asm 的解决办法
    Linux (CentOS7) 下Jdk1.8下载与安装教程
    ffmpeg 图片转视频
    ffmpeg合并多个视频
    ffmpeg 一张图片转视频
    OpenCV图像修复
    ffmpeg 基本命令行
    使用WinSCP从Linux向Windows传送大文件
  • 原文地址:https://www.cnblogs.com/max404/p/10584614.html
Copyright © 2011-2022 走看看