python学习笔记:string的首尾操作

(一)删除首尾特定字符:

1、S.strip([chars]):删除字符串S首尾特定字符(序列)chars,默认为空格

2、S.lstrip([chars]):删除字符串S开头特定字符(序列)chars,默认为空格;

3、S.rstrip([chars]):删除字符串S尾部特定字符(序列)chars,默认为空格。

(二)判断首尾字符:

1、S.startswith(sub,start=num1,end=num2):检查字符串S[start,end]是否是以指定子字符串sub开头

2、S.endswith(suffix,start=num1,end=num2):检查字符串S[start,end]是否是以指定子字符串suffix结尾

print("Dogs and cats!".startswith("Do"))    # True
print("Dogs and cats!".startswith("Don't")) # False
print("-------")
print("Dogs and cats!".endswith("!"))       # True
print("Dogs and cats!".endswith("rats!"))   # False

猜你喜欢

转载自blog.csdn.net/xiaozhimonica/article/details/84936857
今日推荐