Python 从字符串开始提取字符串
我有一个网址:Python 从字符串开始提取字符串,python,python-3.x,Python,Python 3.x,我有一个网址: /c/s/www.local.com/pure/123/india/news/hunt-still-on-for-biker-who-gunned-down-man 我想提取从/pure开始的字符串: /pure/123/india/news/hunt-still-on-for-biker-who-gunned-down-man 在python中最好的方法是什么。使用index()和[]符号 str = "/c/s/www.local.com/pure/123/in
/c/s/www.local.com/pure/123/india/news/hunt-still-on-for-biker-who-gunned-down-man
我想提取从/pure开始的字符串:
/pure/123/india/news/hunt-still-on-for-biker-who-gunned-down-man
在python中最好的方法是什么。使用index()
和[]
符号
str = "/c/s/www.local.com/pure/123/india/news/hunt-still-on-for-biker-who-gunned-down-man"
print(str[str.index("/pure"):])
str[index::
表示从str
开始抓取从index
到字符串末尾的所有字符。这就是您要查找的:
url[url.index('/pure'):]
如果字符串开头的字符数始终相同,则可以忽略第一个字符。 例如,
a_string=“/c/s/www.local.com/pure/123/india/news/hunt仍然在为枪杀该男子的摩托车手进行搜索”
打印(字符串[24:])
您可以考虑正则表达式,以及其他方法。
<代码> S[S-索引(“/纯”):] /代码>字符串类型已经以各种方式以子串的形式查找、拆分或分区。到目前为止你试过什么?您希望在边缘情况下有什么行为,例如字符串中没有出现/pure
?