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

    字符串常用方法

    capitalize()

    String.capitalize() 将字符串首字母变为大写

    1. name = 'xiaoming'
    2.  
    3. new_name = name.capitalize()
    4.  
    5. print(new_name)

       

    运行结果:
    Xiaoming

     

    count()

    String.count() 统计字符出现的次数

    1. name = 'xiaoming'
    2.  
    3. name_num = name.count('i')
    4.  
    5. print(name_num) # 2

       

    center()

    String.center()

    #打印输出字符,让字符串放在中间

    1. name = 'Libai'
    2. print(name.center(50,'*'))

       

    输出结果如下:

    **********************Libai***********************

     

    endswith()

    String.endswith() 判断是否以指定的字符串结尾

    1. name = 'Libai'
    2.  
    3. new_val = name.endswith('bai')
    4. print(new_val)

    结果为:
    True

     

    find()

    String.find() 查找字符串在原字符串中的位置,返回所在索引值

    1. name = 'this is test plaintext'
    2.  
    3. print(name.find('this'))
    4. print(name.find('is'))

       

    在find()方法中,同样可以使用切片。

    1. name = 'this is test plaintext'
    2.  
    3. test_val = name[name.find('test'):12]
    4.  
    5. print(test_val) #test

       

    字符串的切片用法与列表的使用方式一致。

     

    format()

    1. String.format() 输出指定的内容
    2. user_show_name = 'hello,{name},welcome to here,do you like ,{name}'
    3.  
    4. print(user_show_name.format(name='yanyan'))

       

    输出效果如下:

    hello,yanyan,welcome to here,do you like ,yanyan

     

     

    format_map()

    1. String.format_map() 将字典中的参数传递进字符串中,输出
    2. hello = "My name is {name},I am {age} years old.I like {hobby}"
    3.  
    4. # 使用format_map()方法来传递值
    5. print(hello.format_map({'name':'yanyan','age':19,'hobby':'music travel'}))

       

    isalnum()

    String.isalnum() 判断字符串中是否全部为数字或者英文

    1. test_str01 = 'helloIam19yearsold'
    2. test_str02 = 'hello,I am 19 years old'

     

     

    print(test_str01.isalnum()) # True

    print(test_str02.isalnum()) # False

       

    isalnum()方法判断字符串中是否全部为数字或者英文,符合就返回True,不符合就返回False,如果里面包含有符号或者空格之类的特殊字符也会返回False。

     

    isalpha()

    String.isalpha() 判断字符串中是否全部为纯英文字符

    1. test_str03 = 'hello I love you'
    2. test_str04 = 'helloILoveYou'

     

    print(test_str03.isalpha()) # False

    print(test_str04.isalpha()) # True

       

    isdigit()

    String.isdigit() 判断字符串中是否全部为整数

    # isdigit() 判断是否为整数

    1. print('123'.isdigit()) # True
    2. print('hello'.isdigit()) # False

       

    isidentifier()

    String.isidentifier() 判断是不是一个合法的标识符

    # isidentifier() 判断是不是一个合法的标识符

    1. print('test'.isidentifier()) # True
    2. print('12'.isidentifier()) # False
    3. print('_aa'.isidentifier()) # True

       

    判断字符串是否全部为大写或者小写

    islower

    # islower() 判断字符串是否全部是小写

    1. print('Hello,world'.islower()) # False

     

     

    isupper

    # isupper() 判断字符串是否全部为大写

    1. print('Hello,world'.isupper()) # False

       

    join()

    sep.join(seq) 连接字符串数组。将字符串、元组、列表中的元素以指定的字符(分隔符)连接生成一个新的字符串

    1. # 创建一个列表
    2. name = ['张学友','刘德华','郭富城','黎明']
    3.  
    4. print('--'.join(name))

       

    输出结果如下:

    张学友--刘德华--郭富城--黎明

     

    ljust()

    String.ljust(size,替换符号) 从前向后开始计算,当字符串的长度超过size时,超过部分用替换符号替代

     

    rjust()

    String.rjust(size,替换符号) 从后向前开始计算,当字符串的长度超过size时,超过部分用替换符号替代

     

    lower 将字符串大写变成小写

    String.lower()

    1. # 创建一个字符串
    2. str = "hello,I am LiBai,I am 23 years old ,I like travel"
    3.  
    4. # lower 将字符串大写变成小写
    5. print(str.lower())

       

    upper 将字符串小写变成大写

    String.upper()

    1. # 创建一个字符串
    2. str = "hello,I am LiBai,I am 23 years old ,I like travel"
    3.  
    4. # 将字符串小写变成大写
    5. print(str.upper())

       

    Tip:上面的lower()方法和upper()方法改变字符串后将改变的结果返回,但是原本的字符串并不会改变。

     

    lstrip 去掉字符串左边的空格或者回车

    String.lstrip()

    1. print('-----------')
    2. # 创建一个字符串
    3. str = " hello,I am LiBai,I am 23 years old ,I like travel"
    4.  
    5. print(str.lstrip())

       

    输出结果如下:

    -----------

    hello,I am LiBai,I am 23 years old ,I like travel

    除了lstrip 还有rstrip和 strip方法。

     

    replace 替换

    String.replace(old,new,count) 将字符串中的old字符替换为New字符,count为替换的个数

    1. str = 'hello,world,hello'
    2.  
    3. print(str.replace('hello','Hello',1))

       

    输出的效果如下:
    Hello,world,hello

    split

    String.split() 切割

     

    str = 'hello,world,hello'

     

    1. # 默认以空格为分割
    2. print(str.split()) # ['hello,world,hello'] 单词之间没有空格,所以所有的内容为一个元素
    3. # 以o为分割
    4. print(str.split('o')) # ['hell', ',w', 'rld,hell', '']
    5. # 以逗号分割
    6. print(str.split(',')) # ['hello', 'world', 'hello']

     

       

    splitlines() 以换行为分割

    String.splitlines()

    1. str = 'hello, world, hello'
    2.  
    3. print(str.splitlines()) # ['hello,', 'world,', 'hello']

       

       

       

    Tip:补充,python中的字符串并不允许修改值,只允许覆盖值。

    情况如下:

    1. # 创建字符串
    2. str = 'hello,world'
    3. print(str[0]) # h
    4.  
    5. # 尝试去修改
    6. str[0] = 'H'
    7. print(str) # TypeError: 'str' object does not support item assignment
    8.  
    9.  
    10. # 下面这种情况是我们常见的情况,其实是属于一种字符串之前的值被新的值覆盖掉了
    11. str = 'Hello,YanYan'
    12. print(str) # Hello,YanYan

     

     

     

     

     

     

     

     

     

     

     

     

    总结

    1. name = " Ian is very good at {st} learning and eager to learn {sm2}."
    2.  
    3. print(name.capitalize()) #原本大写改成小写,原本小写改成大写
    4.  
    5. print(name.count("a")) #查找里面字符"a"的数量
    6.  
    7. print(name.center(100,"-")) #第一个数字为总共打印多少字符,不够用-补充
    8.  
    9. print(name.ljust(100,'*')) #左边的填充符号
    10.  
    11. print(name.rjust(100,'*')) #右边的填充符号
    12.  
    13. print(name.expandtabs(tabsize=20)) #把tab符号的长度限定为20字节
    14.  
    15. print(name.endswith("rn")) #这个是一个判断输出是,True and False
    16.  
    17. print(name.find("is")) #查找是第几个字符开始
    18.  
    19. print(name[name.find("is"):]) #这个就可以切片了
    20.  
    21. print(name.format(st = 'learning',sm2 = 'organization')) #就是以前学过的添加
    22.  
    23. #name.encode() #将字符串编码成bytes格式
    24.  
    25. print('asd213'.isalpha()) #判断是否为纯英文字符
    26.  
    27. print('asd213'.isalnum()) #判断是否为英文字母以及0-9
    28.  
    29. print('asd2221'.isdigit()) #重点,判断是否为整数
    30.  
    31. print('Aa1'.isidentifier()) #判断是否是合法的标识符,满足Python命名的变量名规则
    32.  
    33. print('sdf'.islower()) #判断是否是小写
    34.  
    35. print('ASA'.isupper()) #判断是否是全是大写
    36.  
    37. print('Asdf'.isnumeric()) #判断是否只包含数字
    38.  
    39. print('Asdf'.istitle()) #判断是否每个首字母大写
    40.  
    41. print('| '.join(['daf','fdaf','dfa'])) #很常见,将前面的字符加到后面的列表的中间
    42.  
    43. print('ALSX'.lower()) #把大写变成小写
    44.  
    45. print('ALads'.upper()) #把字符全是变成大写
    46.  
    47. print('Asfdf '.strip()) #去掉两边的空格和回车
    48.  
    49. print(' Asfdf '.lstrip()) #去掉左边的空格和回车
    50.  
    51. print('Asfdf '.rstrip()) #去掉右边的空格和回车
    52. print(' Asfdf '.lstrip()) #去掉左边的空格和回车
    53.  
    54. print('Asfdf '.rstrip()) #去掉右边的空格和回车
    55.  
    56. print('dfadda'.replace('a','D',1)) #替换,前面的被后面的替换,如果只想换一个就加一个数字
    57.  
    58. print('ffadsfaadfaaaa'.rfind('f')) #找到最后一个f,并返回是第几位
    59.  
    60. print('dafd fa df'.split(' ')) #利用空格进行分割,变成列表
    61.  
    62. print('VIC YI'.swapcase()) #大写变成小写
    63.  
    64. print('afdfa fad'.title()) #把每个单词首字母大写
    就算是咸鱼,也要做最咸的那条。
  • 相关阅读:
    Python之路【第二篇】:Python基础(8)-Tuple元组
    Python之路【第二篇】:Python基础(7)-列表
    Python之路【第一篇】:Python基础(6)
    Python之路【第一篇】:Python基础(5)
    Python之路【第一篇】:Python基础(4)
    Python之路【第一篇】:Python基础(3)
    Python之路【第一篇】:Python基础(2)
    Python之路【第一篇】:Python基础(1)
    SQL Server优化50法
    四层和七层负载均衡的区别
  • 原文地址:https://www.cnblogs.com/pannnnnn/p/9635477.html
Copyright © 2011-2022 走看看