python 字符串常用操作

    xiaoxiao2022-07-02  84

    字符串常用方法

     

    字符串相加:

    word="Py"+"thon" #结果为: Python

     字符串的索引:

    word[0] #为P word[-1] #为n word[1] #为y

     字符串切片:

    word[0:1] #结果为: Py word[2:] #结果为: thon word[-2:-1] #结果为: on

     计算字符串的长度:

    len(word) #结果为: 6

     

     

    capitalize()

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

    name = 'xiaoming' new_name = name.capitalize() print(new_name)

    运行结果: Xiaoming

     

    count()

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

    name = 'xiaoming' name_num = name.count('i') print(name_num) # 2

     

     

    center()

    String.center() #打印输出字符,让字符串放在中间

    #打印输出字符,让字符串放在中间 name = 'Libai' print(name.center(50,'*'))

    输出结果如下:

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

     

     

    endswith()

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

    name = 'Libai' new_val = name.endswith('bai') print(new_val)

    结果为: True

     

    find()

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

    name = 'this is test plaintext' print(name.find('this')) print(name.find('is')) 输出结果为: 0 2

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

    name = 'this is test plaintext' test_val = name[name.find('test'):12] print(test_val) #test

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

     

     

    format()

    String.format() 输出指定的内容,即格式化字符串,格式化字符串还有另外一种方式就是%形式

    user_show_name = 'hello,{name},welcome to here,do you like ,{name}' print(user_show_name.format(name='yanyan'))

    输出效果如下:

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

     

     

    format_map()

    String.format_map() 将字典中的参数传递进字符串中,输出

    hello = "My name is {name},I am {age} years old.I like {hobby}" # 使用format_map()方法来传递值 print(hello.format_map({'name':'yanyan','age':19,'hobby':'music travel'}))

     

    isalnum()

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

    test_str01 = 'helloIam19yearsold' 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() 判断字符串中是否全部为纯英文字符

    test_str03 = 'hello I love you' test_str04 = 'helloILoveYou' print(test_str03.isalpha()) # False print(test_str04.isalpha()) # True

     

    isdigit()

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

    # isdigit() 判断是否为整数 print('123'.isdigit()) # True print('hello'.isdigit()) # False

     

    isidentifier()

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

    # isidentifier() 判断是不是一个合法的标识符 print('test'.isidentifier()) # True print('12'.isidentifier()) # False print('_aa'.isidentifier()) # True

     

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

    # islower() 判断字符串是否全部是小写 print('Hello,world'.islower()) # False # isupper() 判断字符串是否全部为大写 print('Hello,world'.isupper()) # False

     

    join()

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

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

    输出结果如下:

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

     

    ljust()

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

    rjust()

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

     

     

    lower 将字符串大写变成小写

    String.lower()

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

     

    upper 将字符串小写变成大写

    String.upper()

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

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

     

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

    String.lstrip()

    print('-----------') # 创建一个字符串 str = "\nhello,I am LiBai,I am 23 years old ,I like travel" 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为替换的个数

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

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

     

    split

    String.split() 切割

     

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

     

     

    splitlines() 以换行为分割

    String.splitlines()

    str = 'hello,\nworld,\nhello' print(str.splitlines()) # ['hello,', 'world,', 'hello']

     

     

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

    情况如下:

     

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

     

    最新回复(0)