hello_str = "hello world"
# 1. 判断空白字符
space_str = "      \t\n\r"
print(space_str.isspace())

# 2. 判断是否以指定字符串开始
print(hello_str.startswith("hello"))

# 3. 判断是否已指定字符串结束
print(hello_str.endswith("world"))

# 4. 查找字符串
print(hello_str.find("llo"))
print(hello_str.find("abc"))# find 指定字符串不存在会返回-1
# 注意:index 指定字符串不存在会报错

# 5. 替换字符串
# replace方法执行完成后,会返回一个新的字符串
# 注意:不会修改原有字符的内容
print(hello_str.replace("world", "python"))
print(hello_str)

输出:
True
True
True
2
-1
hello python
hello world

10-04 12:02