Python——字符串处理(持续更新)

子串判断

1. 判断 x 是否为 y 的子串

x = 'cde'
y = 'abcdefg'
if x in y:
    print('x is a substring of y.')
# Result:
# x is a substring of y.

2. 判断 x 是否为 y 的前缀

if y.startswith(x):
    print('x is a prefix of y.')

3. 判断 x 是否为 y 的后缀

if x.endswith('y'):
    print('x is a suffix of y.')

字符串查找

1. 查找 x 在 y 中的位置

在这里插入代码片

你可能感兴趣的:(python)