Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to split strings into text and number?

I'd like to split strings like these

'foofo21' 'bar432' 'foobar12345' 

into

['foofo', '21'] ['bar', '432'] ['foobar', '12345'] 

Does somebody know an easy and simple way to do this in python?

like image 459
domruf Avatar asked Jan 09 '09 23:01

domruf


People also ask

How do you split a string into numbers?

To split a string into a list of integers: Use the str. split() method to split the string into a list of strings. Use the map() function to convert each string into an integer.

How do you split text and numbers in Python?

The split() method splits a string into a list. You can specify the separator, default separator is any whitespace. Note: When maxsplit is specified, the list will contain the specified number of elements plus one.

How do I split a string into a list of words?

To convert a string in a list of words, you just need to split it on whitespace. You can use split() from the string class. The default delimiter for this method is whitespace, i.e., when called on a string, it'll split that string at whitespace characters.


1 Answers

I would approach this by using re.match in the following way:

import re match = re.match(r"([a-z]+)([0-9]+)", 'foofo21', re.I) if match:     items = match.groups() print(items) >> ("foofo", "21") 
like image 182
Evan Fosmark Avatar answered Nov 15 '22 22:11

Evan Fosmark