Python endswith()方法
描述
Python endswith() 方法用于判断字符串是否以指定后缀结尾,如果以指定后缀结尾返回True,否则返回False。可选参数"start"与"end"为检索字符串的开始与结束位置。
语法
endswith()方法语法:
str.endswith(suffix[, start[, end]])
参数
- suffix -- 该参数可以是一个字符串或者是一个元素。
- start -- 字符串中的开始位置。
- end -- 字符中结束位置。
返回值
如果字符串含有指定的后缀返回True,否则返回False。
实例
以下实例展示了endswith()方法的实例:
实例(Python 2.0+)
#!/usr/bin/python
str = "this is string example....wow!!!";
suffix = "wow!!!";
print str.endswith(suffix);
print str.endswith(suffix,20);
suffix = "is";
print str.endswith(suffix, 2, 4);
print str.endswith(suffix, 2, 6);
以上实例输出结果如下:
True True True False
Python 字符串



Apathy
cha***gdream1994@gmail.com
应用实例:
#!/usr/bin/python # -*- coding: UTF-8 -*- # 输入一个字符串返回满足以下条件的字符串 # 如果字符串长度大等于3,添加 'ing' 到字符串的末尾 # 如果字符串是以 'ing' 结尾的,就在末尾添加 'ly' # 如果字符串长度小于3,返回原字符串 str = input("请输入一个字符串:") length = len(str) if str.endswith('ing'): str += 'ly' elif length >= 3: str += 'ing' else: pass print(str)Apathy
cha***gdream1994@gmail.com