Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python :: remove all occurrences until first space

Tags:

python

string

how do I remove all occurrences in a string up to the first space, so that:

strings = ["1234 zoocore", "4356 00's punk"]

becomes: ["zoocore", "00's punk"] ?

I have tried regex:

for s in strings:
    new_s = re.sub(r'\d+','', s)

but that erases 00' as well, which I don't want.

like image 886
8-Bit Borges Avatar asked Aug 31 '26 03:08

8-Bit Borges


1 Answers

You can use str.split with maxsplit parameter:

>>> strings = ["1234 zoocore", "4356 00's punk"]
>>> [s.split(None, 1)[1] for s in strings]
['zoocore', "00's punk"]

If you have strings that don't contain space you can use -1 as index:

>>> strings = ["1234 zoocore", "4356 00's punk", "rock"]
>>> [s.split(None, 1)[-1] for s in strings]
['zoocore', "00's punk", 'rock']
like image 81
niemmi Avatar answered Sep 01 '26 16:09

niemmi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!