Separate alphabets and numbers in a string using regular expressions in Python
In this tutorial, we will learn regular expressions to separate numbers and alphabets in Python in different ways. This is very useful in competitive programming. Using regular expressions the process becomes very easy. So let’s begin the tutorial.
To use regular expressions we should import regular expressions library. This is done in the following way
We will consider the following string for the demonstration purpose.
For separation, we will use the findall() method. This method returns a list of all non-overlapping matches in a string. The string is parsed from left to right. This method has 3 arguments.
re.findall(pattern, string, flags=0)
Here, the pattern is the pattern that the user wants from the given string.
import re str="co32despee208dy" n=re.findall(r'\d',str) a=re.findall(r'[a-zA-Z]',str) print(n) print(a)
Here, ‘\d’ is used to find all the digits in the given string. ‘[a-zA-Z]’ is used to find all alphabets from the given string.
['3', '2', '2', '0', '8'] ['c', 'o', 'd', 'e', 's', 'p', 'e', 'e', 'd', 'y']'
We will get 2 lists, one consisting of numbers and the other consisting of alphabets.
import re str="co32despee208dy" n=re.findall(r'\d+',str) a=re.findall(r'[a-zA-Z]+',str) print(n) print(a)
['32', '208'] ['co', 'despee', 'dy']
Here, we will get 2 lists, the first one consisting of numbers. In this list, the numbers which are present without any characters in between are grouped as a single number. The same is the case of the second list which consists of alphabets. If there are no numbers in between, the alphabets are grouped as a string.
import re str="co32despee208dy" n=re.findall(r'\d*',str) a=re.findall(r'[a-zA-Z]*',str) print(n) print(a)
['', '', '32', '', '', '', '', '', '', '208', '', '', ''] ['co', '', '', 'despee', '', '', '', 'dy', '']
Here, the output consists of 2 lists. The first list consists of numbers, only the index places where there is a number, are filled with a number, all the other indexes are filled with empty characters. The same is the case of alphabets in the second list.