截取字符串

# 切片  步长写-1时,表反向输出
str_yuan = "运命重尊,然自其顺的余其,的做能己自做"
# 倒叙
str1 = str_yuan[::-1]
# 截取
start_index = str1.find("")
end_index = str1.find(",尊")
result = str1[start_index:end_index]

# 倒叙后的结果
print(str1)
# :做自己能做的,其余的顺其自然,尊重命运
# 截取的结果
print(result)
# :顺其自然

 

# 切片split结果是列表 list["","",""]  步长写-1时,表反向输出
str_yuan = "运命重尊,然自其顺的余其,的做能己自做"
# 第1步 倒序
str1 = str_yuan[::-1]
# 第2步 切片 保留下标1的部分

result1 = str1.split("")[1]
#注意切片时,中文的逗号和英文不是一个,这里需要用中文的符号
# 第3步 用空替换掉  其余
result2 =result1.replace("其余的"," ")

# 倒序后的结果:做自己能做的,其余的顺其自然,尊重命运
print(str1)
# 切片后的结果:其余的顺其自然
print(result1)
# 替换后的结果 :顺其自然
print(result2)

 

posted @ 2023-07-01 19:34  胖豆芽  阅读(8)  评论(0编辑  收藏  举报