0

I want to cut a String such as "0011165.jpg_Fish" to get only Fish, so everything after the "_", how do i do that in python?

Thank you very much!

Sven Prüß
  • 9
  • 1
  • 2

2 Answers2

2

Please use str.partition instead of str.split. This is robust, since you can always expect 3 items, unlike, split which maybe tricky to handle if the input string doesn't have the split character,

>>> word = '0011165.jpg_Fish'
>>> not_required, split_char, required = word.partition('_')
>>> required
'Fish'
han solo
  • 6,390
  • 1
  • 15
  • 19
0

Try

"0011165.jpg_Fish".split("_")[1]

And in case of a Dataframe

train['Label'] = train.Image_Labels.str.split("_").str[1]
Ibtihaj Tahir
  • 636
  • 5
  • 17