Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Splitting string with multiple parameters

Tags:

python

I have a string

Best product25.075.0Product29.029.0

And now I need to split this string to

'Best product' '25.0' '75.0' , 'Product' '29.0' '29.0'

How can i achieve this?

like image 864
Chaban33 Avatar asked Mar 21 '26 16:03

Chaban33


1 Answers

You can use re.findall to find all words (containing letter or space - matching pattern [a-zA-Z ]+) or all numbers (one or more digits followd by a dot and zero - matching pattern \d+.0)

string = 'Best product25.075.0Product29.029.0'
import re
re.findall(r'[a-zA-Z ]+|\d+(?:.0)?', string)
# ['Best product', '25.0', '75.0', 'Product', '29.0', '29.0']
like image 183
Sunitha Avatar answered Mar 23 '26 07:03

Sunitha



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!