""" 模块:python3 str.startswith()_endswith().py 功能:python3 判断字符串是否以某个字串开始、结束。 参考: https://www.runoob.com/python3/python3-string-startswith.html https://www.runoob.com/python3/python3-string-endswith.html """ # 1.str.startswith(prefix[, begindex=0[,endindex=len(string)]]) -> True/False # 检查字符串是否是以指定子字符串开头, # 如果是则返回 True,否则返回 False。 # 如果参数 begindex 和 endindex 指定值,则在指定范围内检查。 # str -- 要检测的字符串。 # prefix -- 指定的子字符串。 # begindex -- 可选参数,用于设置字符串检测的起始位置。 # endindex -- 可选参数,用于设置字符串检测的结束位置。 str1 = "this is string example....wow!!!" print(str1.startswith('this')) # True # str1[8:]字符串是否以 string 开头 print("str1[8:]:", str1[8:]) # str1[8:]: string example....wow!!! print(str1.startswith('string', 8)) # True # str1[2:4]字符串是否以 this 开头 print("str1[2:4]:", str1[2:4]) # str1[2:4]: is print(str1.startswith('this', 2, 4)) # False # 2.str.endswith(suffix[, start[, end]]) -> True/False # 判断字符串是否以指定后缀结尾,如果以指定后缀结尾返回 True,否则返回 False。 # 可选参数 "start" 与 "end" 为检索字符串的开始与结束位置。 # suffix -- 该参数可以是一个字符串或者是一个元素。 # start -- 字符串中的开始位置。 # end -- 字符串中结束位置(不包括该位置的字符)。 # 如果字符串含有指定的后缀返回 True,否则返回 False。 print("\n2.") Str = 'Runoob example....wow!!!' suffix = '!!' print(Str.endswith(suffix)) # True print(Str.endswith(suffix, 20)) # True suffix = 'Run' print(Str.endswith(suffix)) # False print("Str[0:3]:", Str[0:3]) # Str[0:3]: Run print(Str.endswith(suffix, 0, 3)) # True
python3 str.startswith()_endswith().py
最新推荐文章于 2023-11-08 20:40:12 发布