Python 返回具有多个字符对的字符串中特定字符对之间的字符串部分

Python 返回具有多个字符对的字符串中特定字符对之间的字符串部分,python,text-manipulation,Python,Text Manipulation,文本文件或字符串: SomeText1/SomeText2/SomeText3/SomeText4/SomeText5 #我在寻找什么: 拆分函数(3,“/”) >>>SomeText3 试试: s = "SomeText1/SomeText2/SomeText3/SomeText4/SomeText5" # s.split("/") returns a list of strings, split at the "/" # I.e. ["SomeText1", "SomeText2", "So

文本文件或字符串:

SomeText1/SomeText2/SomeText3/SomeText4/SomeText5

#我在寻找什么:
拆分函数(3,“/”)
>>>SomeText3
试试:

s = "SomeText1/SomeText2/SomeText3/SomeText4/SomeText5"
# s.split("/") returns a list of strings, split at the "/"
# I.e. ["SomeText1", "SomeText2", "SomeText3", "SomeText4", "SomeText5"]
# Then take the second element (remembering that the count starts at 0
result = s.split("/")[2]

希望您至少尝试自己编写此代码。我建议你做一些,或者通过谷歌,或者通过搜索,尝试一下。如果仍然有问题,请返回代码并解释您的尝试。如果字符串从未以
/
开头:
def split_func(值,分隔符,索引):返回值。split(“/”,index+1)[index]
(并使用
split_func(字符串值,“/”,3)
)但是,如果这些确实是文件系统路径,然后考虑使用或处理所有不同的角落情况(领导<代码> /<代码>,不同的操作系统约定,共享驱动器等)。限制分割,没有进一步分裂余下的点。