-2

Text file or string:

SomeText1/SomeText2/SomeText3/SomeText4/SomeText5

#What I am looking for:
split_func(3, "/")

>>> SomeText3
Floxxy
  • 7
  • 3
  • 1
    It is expected that you at least attempt to code this for yourself. I would suggest that you do some [additional research](http://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users), either via Google or by searching SO, make an attempt and. if you still have trouble, come back with your code and explain what you have tried. – luis.parravicini Oct 18 '19 at 13:56
  • 1
    If your strings never start with `/`: `def split_func(value, separator, index): return value.split("/", index + 1)[index]` (and call with `split_func(string_value, "/", 3)`) – Martijn Pieters Oct 18 '19 at 13:56
  • 1
    **However**, if these are really filesystem paths, then consider using either [`pathlib`](https://docs.python.org/3/library/pathlib.html) or [`os.path`](https://docs.python.org/3/library/os.path.html) to handle all the different corner cases (leading `/`, different OS conventions, shared drives, etc.). – Martijn Pieters Oct 18 '19 at 13:58

1 Answers1

0

Try:

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]
RightmireM
  • 2,381
  • 2
  • 24
  • 42