简体   繁体   中英

Getting Substring after certain char in python

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!

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'

Try

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

And in case of a Dataframe

train['Label'] = train.Image_Labels.str.split("_").str[1]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM