python字符串

发布时间:2017-11-06 18:10:18编辑:Run阅读(5465)

    字符串的操作方法很多,这里只选几种常用的

    (1)    字符串大小写转换

    1.    S.lower()    字母大写转换成小写

    2.    S.upper()    字母小写转换成大写

    3.    S.swapcase()    字母大写转换小写,小写转换大写

    4.    S.title()    将首字母大写

    实例代码如下:

    # coding: utf-8
    __author__ = 'www.py3study.com'
    #字母大写转小写
    string_capital = 'WWW.PY3STUDY.COM'
    print(string_capital.lower())
    #字母小写转大写
    string_lowercase = 'www.py3study.com'
    print(string_lowercase.upper())
    #字母大小写互转
    string_Case_rotation = 'Www.Py3study.coM'
    print(string_Case_rotation.swapcase())
    #将首个字母大写
    string_capitalized = 'www.py3study.com'
    print(string_capitalized.title())

    运行效果如下:

    blob.png

    (2)    字符串搜索,替换

    1.    S.find(substr,[start,[end]])    返回S中出现substr的第一个字母的标号,如果S中没有substr则返回-1,start和end作用就相当于在S[start:end]中搜索

    2.    S.count(substr,[start,[end]])    计算substr在S中出现的次数

    3.    S.replace(oldstr, newstr, [count])    把S中的oldstr替换为newstr,count为替换次数

    4.    S.strip([chars])    把S左右两端chars中有的字符全部去掉,一般用于去除空格

    5.    S.lstrip([chars])    把S左端chars中所有的字符全部去掉

    6.    S.rstrip([chars])    把S右端chars中所有的字符全部去掉

    实例代码如下:

    # coding: utf-8
    __author__ = 'www.py3study.com'
    string = 'www.py3study.com'
    strings = ' www.py3study.com '
    #返回string中m的第一个字母标记
    print(string.find('m',0, len(string)))
    #计算string中w出现的次数
    print(string.count('w',0,len(string)))
    #把string中的com替换为net,1为替换次数
    print(string.replace('com','net',1))
    #把strings中左右两端的空格去掉,' '指空格
    print(strings.strip(' '))
    #把string中左端中的www字符去掉
    print(string.lstrip('www'))
    #把string中右端的com去掉
    print(string.rstrip('com'))

    运行效果

    blob.png

    (3)    字符串分割,组合

    1.    S.split([sep, [maxsplit]])    以sep为分隔符,把S分成一个list,maxsplit表示分割的次数,默认的分隔符为空白字符

    2.    S.join(seq)    把seq代表的序列---字符串序列,用S连接起来

    实例代码

    # coding: utf-8
    __author__ = 'www.py3study.com'
    string = 'www.py3study.com'
    strs = '.'
    seq = ('www','py3study','com')
    #以w为分隔符,分割3次,把string分割成一个list
    print(string.split('w',3))
    #以点为连接符,把seq里面的字符串连接起来
    print(strs.join(seq))

    运行效果

    blob.png

    (4)    字符串编码,解码

    1.    S.decode([encoding])    将以encoding编码的S解码成unicode编码

    2.    S.encode([encoding])    将以unicode编码的S编码成encoding,encoding可以是gb2313,gbk,utf8

    代码如下:

    # coding: utf-8
    __author__ = 'www.py3study.com'
    string = '你好!python'
    print(string.encode('utf-8'))
    print(string.encode('GBK'))
    utf8_string = string.encode('utf-8')
    gbk_string = string.encode('GBK')
    print(type(utf8_string))
    print(type(gbk_string))
    print(utf8_string.decode('utf-8'))
    print(gbk_string.decode('GBK'))

    运行结果

    blob.png

    (5)    字符串测试

    1.    S.isalpha()    S是否全是字母,至少有一个字符

    2.    S.isdigit()    S是否全是数字,至少有一个字符

    3.    S.isspace()    S是否全是空白字符,至少有一个字符

    4.    S.islower()    S中的字母是否全是小写

    5.    S.isupper()    S中的字母是否全是大写

    6.    S.istitle()    S是否是首字母大写的


    编写一个showstr.py来实验一下

    # coding: utf-8
    __author__ = 'www.py3study.com'
    def strcase():
        '''字符串转换'''
        print('演示字符串大小写转换')
        print('演示的S值为:WWW.py3study.com')
        S = 'WWW.py3study.com'
        print('大写转小写:tS.lower() = {}'.format(S.lower()))
        print('小写转大写:tS.upper() = {}'.format(S.upper()))
        print('大小写转换:tS.swapcase() = {}'.format(S.swapcase()))
        print('首字母大写:tS.title() = {}'.format(S.title()))
        print('\n')
    
    def strfind():
        '''字符串搜索,替换'''
        print('演示字符串搜索,替换等')
        print('演示的S值为: www.py3study.com ')
        S = ' www.py3study.com '
        print('字符串搜索:tS.find("m") = {}'.format(S.find('m',0,len(S))))
        print('字符串统计:tS.count("w") = {}'.format(S.count('w',0,len(S))))
        print('字符串替换:tS.replace("com","net") = {}'.format(S.replace('com','net')))
        print('字符串去左右空格:tS.strip() = {}'.format(S.strip()))
        print('字符串去左空格加www:tS.lstrip().lstrip("www") = {}'.format(S.lstrip().lstrip('www')))
        print('字符串去右边空格加com:tS.rstrip().rstrip("com") = {}'.format(S.rstrip().rstrip('com')))
        print('\n')
    
    def strsplit():
        '''字符串分割,组合'''
        print('演示字符串分割,组合')
        print('演示的S值为:www.py3study.com')
        S = 'www.py3study.com'
        S_temp = '.'
        print('字符串分割w3次:tS.split() = {}'.format(S.split('w',3)))
        print('字符串组合(1)以.(点)连接:t".".join(["www","study","com"]) = {}'.format('.'.join(['www','study','com'])))
        print('字符串组合(2)以$符连接:t"$".join(["www","study","com"]) = {}'.format('$'.join(['www','study','com'])))
        print('字符串组合(3)以空格链接:t" ".join(["www","study","com"]) = {}'.format(' '.join(['www','study','com'])))
        print('\n')
    
    def strcode():
        '''字符串编码,解码'''
        print("演示字符串编码,解码")
        print("演示字符串S赋值为:'编码解码测试'")
        S = '编码解码测试'
        print('GBK编码的S:t = {}'.format(S))
        print('GBK编码的S转换为GBK编码')
        print('S.encode("GBK") = {}'.format(S.encode('GBK')))
        gbk_string = S.encode('GBK')
        print('GBK解码为汉字')
        print(gbk_string.decode('GBK'))
        print('GBK编码的S转换为UTF8编码')
        print('S.encode("UTF8") = {}'.format(S.encode('utf-8')))
        utf8_string = S.encode('utf-8')
        print('utf8解码为汉字')
        print(utf8_string.decode('utf-8'))
        print('\n')
    
    def strtest():
        '''字符串测试'''
        print('演示字符串S赋值为:"study"')
        S = 'study'
        print('测试是否全是字母S.isalpha() = {}'.format(S.isalpha()))
        print('测试是否全是数字S.isdigit() = {}'.format(S.isdigit()))
        print('测试全是空白字符,至少有一个S.isspace() = {}'.format(S.isspace()))
        print('测试是否全是小写字母S.islower() = {}'.format(S.islower()))
        print('测试是否全是大写字符S.isupper() = {}'.format(S.isupper()))
        print('测试是否首字母大写S.istitle() = {}'.format(S.istitle()))
        print('\n')
    
    if __name__ == '__main__':
        strcase()
        strfind()
        strsplit()
        strcode()
        strtest()

    运行结果

    "C:Program Files (x86)python3.6python.exe" D:/python3_study/str4.py

    演示字符串大小写转换

    演示的S值为:WWW.py3study.com

    大写转小写:S.lower() = www.py3study.com

    小写转大写:S.upper() = WWW.PY3STUDY.COM

    大小写转换:S.swapcase() = www.PY3STUDY.COM

    首字母大写:S.title() = Www.Py3Study.Com



    演示字符串搜索,替换等

    演示的S值为: www.py3study.com 

    字符串搜索:S.find("m") = 16

    字符串统计:S.count("w") = 3

    字符串替换:S.replace("com","net") =  www.py3study.net 

    字符串去左右空格:S.strip() = www.py3study.com

    字符串去左空格加www:S.lstrip().lstrip("www") = .py3study.com 

    字符串去右边空格加com:S.rstrip().rstrip("com") =  www.py3study.



    演示字符串分割,组合

    演示的S值为:www.py3study.com

    字符串分割w3次:S.split() = ['', '', '', '.py3study.com']

    字符串组合(1)以.(点)连接:".".join(["www","study","com"]) = www.study.com

    字符串组合(2)以$符连接:"$".join(["www","study","com"]) = www$study$com

    字符串组合(3)以空格链接:" ".join(["www","study","com"]) = www study com



    演示字符串编码,解码

    演示字符串S赋值为:'编码解码测试'

    GBK编码的S: = 编码解码测试

    GBK编码的S转换为GBK编码

    S.encode("GBK") = b'xb1xe0xc2xebxbdxe2xc2xebxb2xe2xcaxd4'

    GBK解码为汉字

    编码解码测试

    GBK编码的S转换为UTF8编码

    S.encode("UTF8") = b'xe7xbcx96xe7xa0x81xe8xa7xa3xe7xa0x81xe6xb5x8bxe8xafx95'

    utf8解码为汉字

    编码解码测试



    演示字符串S赋值为:"study"

    测试是否全是字母S.isalpha() = True

    测试是否全是数字S.isdigit() = False

    测试全是空白字符,至少有一个S.isspace() = False

    测试是否全是小写字母S.islower() = True

    测试是否全是大写字符S.isupper() = False

    测试是否首字母大写S.istitle() = False



关键字

上一篇: Python变量类型

下一篇: python列表