学Python必备的15个字符串方法,你学废了吗?

x33g5p2x  于2022-03-05 转载在 Python  
字(2.9k)|赞(0)|评价(0)|浏览(210)

兄弟们Python都学的怎么样了?字符串学会了么?

字符串是Python最基本的数据类型,遍布所有Python程序,你要你在用用Python,就都会使用到它。

所以总结了15个最重要的内置字符串方法分享给大家,希望大家能从中找到对自己有帮助的技巧。

1、isalnum()

如果字符串中至少有一个字符并且所有字符都是字母或数字,则返回True,否则返回 False。

s = 'python'
print(s.isalnum())
# True

s = '123'
print(s.isalnum())
# True

s = 'python123'
print(s.isalnum())
# True

s = 'python-123'
print(s.isalnum())
# False

2、find()

检测指定内容是否包含在字符串中,如果是返回开始的索引值,否则返回-1。

s = 'Machine Learning'
idx = s.find('a')

print(idx)
print(s[idx:])
# 1# achine Learning

s = 'Machine Learning'
idx = s.find('aa')

print(idx)
print(s[idx:])
# -1# g

此外,还可以指定开始的范围。

s = 'Machine Learning'
idx = s.find('a', 2)

print(idx)
print(s[idx:])
# 10
# arning

3、count()

返回指定内容在字符串中出现的次数。

n = 'hello world'.count('o')
print(n)
# 2

n = 'hello world'.count('oo')
print(n)
# 0

4、join()

string.join(seq)。以string作为分隔符,将seq中所有的元素(的字符串表示)合并为一个新的字符串。

list_of_strings = ['string', 'methods', 'in', 'python']
s = '-'.join(list_of_strings)

print(s)
# string-methods-in-python

list_of_strings = ['string', 'methods', 'in', 'python']
s = ' '.join(list_of_strings)

print(s)
# string methods in python

5、rsplit()

从右侧开始对字符串进行分隔

s = 'string methods in python'.rsplit(' ', maxsplit=1)

print(s)
# ['string methods in', 'python']

6、Slicing

slicing切片,按照一定条件从列表或者元组中取出部分元素(比如特定范围、索引、分割值)

s = '   hello   '
s = s[:]

print(s)
#    hello

s = '   hello   '
s = s[3:8]

print(s)
# hello

7、strip()

strip()方法用于移除字符串头尾指定的字符(默认为空格或换行符)或字符序列。

s = '   hello   '.strip()

print(s)
# hello

s = '###hello###'.strip()

print(s)
# ###hello###

在使用strip()方法时,默认去除空格或换行符,所以#号并没有去除。

可以给strip()方法添加指定字符,如下所示。

s = '###hello###'.strip('#')

print(s)
# hello

此外当指定内容不在头尾处时,并不会被去除。

s = ' \n \t hello\n'.strip('\n')

print(s)
#
#      hello

s = '\n \t hello\n'.strip('\n')

print(s)
#      hello

第一个\n前有个空格,所以只会去取尾部的换行符。

最后strip()方法的参数是剥离其值的所有组合,这个可以看下面这个案例。

s = 'www.baidu.com'.strip('cmow.')

print(s)
# baidu

最外层的首字符和尾字符参数值将从字符串中剥离。字符从前端移除,直到到达一个不包含在字符集中的字符串字符为止。

在尾部也会发生类似的动作

8、lstrip()

移除字符串左侧指定的字符(默认为空格或换行符)或字符序列。

s = '   hello   '.lstrip()

print(s)
# hello

同样的,可以移除左侧所有包含在字符集中的字符串。

s = 'Arthur: three!'.lstrip('Arthur: ')

print(s)
# ee!

9、rstrip()

移除字符串右侧指定的字符(默认为空格或换行符)或字符序列。

s = '   hello   '.rstrip()

print(s)
#    hello

10、removeprefix()

Python3.9中移除前缀的函数。

# python 3.9
s = 'Arthur: three!'.removeprefix('Arthur: ')

print(s)
# three!

和strip()相比,并不会把字符集中的字符串进行逐个匹配。

11、removesuffix()

Python3.9中移除后缀的函数。

s = 'HelloPython'.removesuffix('Python')

print(s)
# Hello

12、replace()

把字符串中的内容替换成指定的内容。

s = 'string methods in python'.replace(' ', '-')

print(s)
# string-methods-in-python

s = 'string methods in python'.replace(' ', '')

print(s)
# stringmethodsinpython

13、re.sub()

re是正则的表达式,sub是substitute表示替换。

re.sub则是相对复杂点的替换。

import re
s = "string    methods in python"
s2 = s.replace(' ', '-')
print(s2)
# string----methods-in-python

s = "string    methods in python"
s2 = re.sub("\s+", "-", s)
print(s2)
# string-methods-in-python

和replace()做对比,使用re.sub()进行替换操作,确实更高级点。

14、split()

对字符串做分隔处理,最终的结果是一个列表。

s = 'string methods in python'.split()

print(s)
# ['string', 'methods', 'in', 'python']

当不指定分隔符时,默认按空格分隔。

s = 'string methods in python'.split(',')

print(s)
# ['string methods in python']

15、upper()

将字符串中的字母,全部转换为小写。

s = 'SIMPLE IS BETTER THAN COMPLEX'.lower()

print(s)
# simple is better than complex

兄弟们,今天的分享就到这里,大家学废了吗?

学习难,难在坚持!与诸君共勉!加油!

相关文章