Files
2025PY/day20/05-字符串方法.py
2025-05-22 16:50:44 +08:00

32 lines
814 B
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 字符串的方法
# 1、str.startswith(a, start, end)在[start:end]区间可省略以a开头返回True, 否则False
str='hello china'
# 没有区间
print(str.startswith('h')) #True
print(str.startswith('he')) #True
print(str.startswith('hello')) #True
print(str.startswith('c')) #False
# 设置区间
print(str.startswith('c',6)) #True 索引6位置开始
# 2、str.endswith(a, start, end) 在[start:end]区间可省略以a结尾返回True, 否则False
str='hello china'
# 没有区间
print(str.endswith('a')) #True
print(str.endswith('na')) #True
print(str.endswith('china')) #True
print(str.endswith('o')) #False
# 设置区间
print(str.endswith('o',0,5)) #True 区间不包括结尾位置 o的索引是5
print(str.endswith('i',0,9)) #True 区间不包括结尾位置 i的索引是9