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.

re Library

To use regular expressions we should import regular expressions library. This is done in the following way

import re

re.findall()

We will consider the following string for the demonstration purpose.

“co32despee208dy”

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.

Example 1

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.

OUTPUT:

['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.

Example 2

import re
str="co32despee208dy"
n=re.findall(r'\d+',str)
a=re.findall(r'[a-zA-Z]+',str)
print(n)
print(a)

OUTPUT:

['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.

Example 3

import re
str="co32despee208dy"
n=re.findall(r'\d*',str)
a=re.findall(r'[a-zA-Z]*',str)
print(n)
print(a)

OUTPUT:

['', '', '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.

One response to “Separate alphabets and numbers in a string using regular expressions in Python”

  1. Nick says:

    what does the r mean that is standalone in the code

Leave a Reply

Your email address will not be published.