Python3-字符串

python字符串内置函数
    一、大小写处理
    二、判断字符串中的字符类型
    三、字符串的替换
    四、去空格
    五、用特定符连接单个字符
    六、用字符串中的特定符分割字符串
    七、搜索
    八、填充
    九、encode()与decode()
    十、.expandtabs()
    十一、格式对齐
 
一、大小写处理:.capitalize()、.upper()、.lower()、.swapcase()、.title()、casefold()
 
    ①    .capitalize()
        作用:首字母大写,其余小写
        示例: str_1 = 'hello World'
               result = str_1.capitalize()
               print(result)
        结果:Hello World
    
    ②    .upper()
        作用:全部大写
        示例:str_1 = 'hello World'
              result = str_1.upper()
              print(result)   
        结果:HELLO WORLD
 
    ③    .lower()
        作用:全部小写
        示例:str_1 = 'HELLO WORLD'
              result = str_1.lower()
              print(result)
        结果:hello world
 
    ④    .casefold()
        作用:和.lower()方法非常相似,都可以转换字符串中大写字符为小写
        区别:.lower()只对ASCII编码,也就是'A-Z'有效,对于其他语言(非汉语或英语)中大写转为小写的情况,只能用.casefold()方法
 
    ⑤    .swapcase()
        作用:大小写互换
        示例:str_1 = 'hello WORLD'
              result = str_1.swapcase()
              print(result)
        结果:HELLO world
    
    ⑥    .title()
        作用:首字母大写
        示例:str_1 = 'hello world'
              result = str_1.title()
              print(result)
        结果:Hello World
        注意:若str_1 = 'helloworld'---->'Helloworld'
 
二、判断字符串中的字符类型
    ①    .startswith('a',[start,end])
        作用:是否以a开头
        示例:str_1 = 'hello world'
              result = str_1.startswith('h')
              print(result)
        结果:True
        注意:若result = str_1.startswith('h',3,6)---->False
 
    ②    .endswith('a',[start,end])
        作用:是否以a结尾
        示例:str_1 = 'hello world'
              result = str_1.endswith('d')
              print(result)
        结果:True
        注意:若result = str_1.endswith('d',3,6)---->False
 
    ③    .isalnum()
        作用:是否全为字母或数字
        示例:str_1 = 'HelloWorld2020'
              result = str_1.isalnum()
              print(result)
        结果:True
        注意:若str_1 = 'Hello World 2020'---->False
 
    ④    .isalpha()
        作用:是否为全字母
        示例:str_1 = 'HelloWorld'
              result = str_1.isalpha()
              print(result)
        结果:True
        注意:若str_1 = 'Hello World'---->False
 
    ⑤    .isdigit()
        作用:是否为全数字
        示例:str_1 = '123456'
              result = str_1.isdigit()
              print(result)
        结果:True
 
    ⑥    .islower()
        作用:是否为全小写
        示例:str_1 = 'helloworld2020~'
              result = str_1.islower()
              print(result)
        结果:True
 
    ⑦    .isupper()
        作用:是否为全大写
        示例:str_1 = 'HELLOWORLD2020~'
              result = str_1.isupper()
              print(result)
        结果:True
 
    ⑧    .istitle()
        作用:判断首字母是否为大写
        示例:str_1 = 'Hello World 2020 ~'
              result = str_1.istitle()
              print(result)
        结果:True
 
    ⑨    .isspace()
        作用:判断字符是否为空格
        示例:str_1 = ' '
              result = str_1.isspace()
              print(result)
        结果:True
        注意:若str_1 = ''---->False
              若str_1 = None---->AttributeError: 'NoneType' object has no attribute 'isspace'
 
三、字符串替换
    ①    .replace('old','new')
        作用:替换old为new
        示例:str_1 = 'Hello World'
              result = str_1.replace('ll','yy')
              print(result)
        结果:Heyyo World
    
    ②    .replace('old','new',次数)
        作用:替换指定次数的old为new
        示例:str_1 = 'Hello World'
              result = str_1.replace('o','y',1)
              print(result)
        结果:Helly World
 
四、去空格
    ①    .strip()
        作用:去两边空格
        示例:str_1 = ' Hello World '
              result = str_1.strip()
              print(result)
        结果:Hello World
    
    ②    .lstrip()
        作用:去左边(left)空格
    
    ③    .rstrip()
        作用:去右边(right)空格
 
五、用特定符连接单个字符
    ①    s.join(iterable)---->str
        作用:返回一个字符串,该字符串是iterable中的字符串的串联。元素之间的分隔符是S。
        示例:str_1 = 'Hello World'
              result = '*'.join(str_1)
              print(result)
        结果:H*e*l*l*o *W*o*r*l*d
 
六、用字符串中的特定符分割字符串
    ①    .split()
        作用:默认按空格分割
        示例:str_1 = 'Hello World'
              result = str_1.split()
              print(result)
        结果:['Hello','World']
    
    ②    .split('指定字符')
        作用:按指定字符分割字符串为数组,如果没有该指定字符,则原样输出
        示例:str_1 = 'Hello World'
              result = str_1.split('l')
              print(result)
        结果:['He','','o Wor','d']
        注意:若result = str_1.split('yy')---->['Hello World']
 
七、搜索
    ①    .find()
        作用:搜索指定字符串,有则返回指定字符串第一次出现时首个字符的下标index,没有返回-1
        示例:str_1 = 'Hello World'
              result = str_1.find('l')
              print(result)
        结果:2
    
    ②    .rfind()
        作用:从右侧开始查找
    
    ③    .index()
        作用:搜索指定字符串,若没有找到则会报错
        示例一:str_1 = 'Hello World'
                result = str_1.index('ll')
                print(result)
        结果  :2
 
        示例二:str_1 = 'Hello World'
                result = str_1.index('yy')
                print(result)
        结果:ValueError: substring not found
 
    ④   .rindex(str,beg=0 end=len(string))
        作用:返回字符串str在字符串中最后出现的位置,如果没有匹配的字符串会报异常
        示例:str_1 = "www.baidu.com"
              result = str_1.rindex('c')
              print(result)
        结果:10
    
    ⑤    .count()
        作用:统计指定的字符串出现的次数,若没有则返回0
 
八、填充
    ①    .center(width[,fillchar])
        作用:返回以长度和宽度为中心的字符串,使用指定的填充字符填充(默认为空格)
              ·width -- 字符串的总宽度
              ·fillchar -- 填充符
        示例:str_1 = 'Hello World'
              result = str_1.center(20,'*')
              print(result)
        结果:****Hello World*****
 
    ②    .zfill(width)
        作用:返回指定长度的字符串,原字符串向右对齐,前面填充0
        示例:str_1 = "www.baidu.com"
              result = str_1.zfill(20)
              print(result)
        结果:0000000www.baidu.com
 
九、.encode()与.decode()
        ·.encode():str ---> bytes    编码
        作用:将字符串转换成字节码,涉及到字符串的内部表示
        示例:str_1 = '你好!'
              result = str_1.encode()
              print(result)
        结果:b'\xe4\xbd\xa0\xe5\xa5\xbd!'
        
        ·.decode():bytes --> str     解码
        作用:将字节码转换成字符串,将比特位显示成字符
        示例:str_1 = b'\xe4\xbd\xa0\xe5\xa5\xbd!'
              result = str_1.decode()
              print(result)
        结果:你好!
 
        声明:decode()和encode()方法可以接受参数,其声明分别为:
                ·str.encode(encoding="utf-8",errors="strict")
                ·bytes.decode(encoding="utf-8",errors="strict")
                其中,encoding是指编解码过程中使用的编码(即"编码方案"),
                      errors是指错误的处理方案
 
十、.expandtabs()
        .expandtabs(tabsize=8)
        作用:把字符串中的tab符号("\t")转为空格,\t默认空格数为8
              ·tabsize -- 指定转换字符串中的\t转为空格的字符数
        示例:str_1 = '你好!\t中国!'
              result = str_1.expandtabs(tabsize=20)
              print(result)
        结果:你好!                 中国!
              
十一、格式对齐
        ①    .partition(str)
        作用:用来根据指定的分隔符将字符串进行分割, 如果字符串包含指定的分隔符,则返回一个3元的元组,第一个为分隔符左边的子串,第二个为分隔符本身,第三个为分隔符右边的子串。
        示例:str_1 = 'www.baidu.com'
              result = str_1.partition()
              print(result)
        结果:('www', '.', 'baidu.com')
    
        ②    .ljust(width[,fillchar])
        作用:返回一个原字符串左对齐,并使用空格填充至指定长度的新字符串。如果指定的长度小于原字符串的长度则返回原字符串。
        示例:str_1 = 'www.baidu.com'
              result = str_1.ljust(20,'*')
              print(result)
        结果:www.baidu.com*******
 
        ③    .rjust(width[,fillchar])
        作用:和.ljust()用法类似,只是方向相反
 
 
 
 
 
 
 
 
 
 
 
posted @ 2020-05-31 12:50  闹点小情绪q  阅读(153)  评论(0编辑  收藏  举报