首页 > 解决方案 > Python从字符串中的开头到第一个特定字符删除?

问题描述

假设我有以下字符串:

this is ;a cool test

如何删除从启动到第一次;发生的所有内容?预期的输出将是a cool test

我只知道如何使用方括号表示法删除固定数量的字符,这在这里没有帮助,因为 的位置;不固定。

标签: pythonstringsplit

解决方案


使用str.find和切片。

前任:

s = "this is ;a cool test; Hello World."
print(s[s.find(";")+1:])
# --> a cool test; Hello World.

或使用str.split

前任:

s = "this is ;a cool test; Hello World."
print(s.split(";", 1)[-1])
# --> a cool test; Hello World.

推荐阅读