How to Separate Alphabets and Numbers in a String using regular expression in Python?

In this article, let us look at how to use regular expressions in Python to split numbers and alphabets in a number of ways. This is extremely helpful in competitive programming. The technique gets quite simple when regular expressions are used.

re library/RegEx Module:

Python includes the re package, which can be used to interact with Regular Expressions.
A RegEx, or Regular Expression, is a character sequence that forms a search pattern.

RegEx can be used to determine whether or not a string contains the specified search pattern.

To work with this library we should first import it.

re.findall() method:

The findall() function to separate things out. This method gives a list of all non-overlapping matches in a string. From left to right, the string is parsed. This method takes three arguments.

re.findall(pattern, string, flags=0)

Parameters

pattern: It is the pattern that the user needs from the given string.

string: It is the string given as input.

Separating Alphabets and Numbers in a String using regular expression in Python

Example1

Approach:

  • Import re module using the import keyword
  • Give the string as static input and store it in a variable
  • Get all the numbers in a given string using findall() function and store it in a variable.
  • Here ‘\d’represents digits.
  • Get all the alphabets in a given string using findall() function and and store it in another variable.
  • Here ‘[a-zA-Z]’ represents alphabets(lower, uppercase)
  • Print all the numbers present in a given string.
  • Print all the alphabets present in a given string.
  • The Exit of the Program.

Below is the implementation:

# Import re module using the import keyword
import re
# Give the string as static input and store it in a variable
gvn_str = "Python1213prog786rams"
# Get all the numbers in a given string using findall() function
# and store it in a variable.
# Here '\d'represents digits.
numbrs = re.findall(r'\d', gvn_str)
# Get all the alphabets in a given string using findall() function and
# and store it in another variable.
# Here '[a-zA-Z]' represents alphabets(lower, uppercase)
alphabets = re.findall(r'[a-zA-Z]', gvn_str)
# Print all the numbers present in a given string
print("The numbers present in a given string{", gvn_str, "}:")
print(numbrs)
# Print all the alphabets present in a given string
print("The alphabets present in a given string{", gvn_str, "}:")
print(alphabets)

Output:

The numbers present in a given string{ Python1213prog786rams }:
['1', '2', '1', '3', '7', '8', '6']
The alphabets present in a given string{ Python1213prog786rams }:
['P', 'y', 't', 'h', 'o', 'n', 'p', 'r', 'o', 'g', 'r', 'a', 'm', 's']

Example2: Separating Numbers and Alphabets in the specified Pattern Format

Here two lists are returned, the first of which will be made up of numbers. The numbers in this list that are present without any characters in between are grouped as a single number. The same is true for the second list, which is made up of alphabets. If there are no numbers between the alphabets, they are combined together as a string.

import re
gvn_str = "Python1213prog786rams"
numbrs = re.findall(r'\d+', gvn_str)
alphabets = re.findall(r'[a-zA-Z]+', gvn_str)
# Print all the numbers present in a given string in the specified pattern format
print(
    "The numbers present in a given string{", gvn_str, "} in the specified pattern:")
print(numbrs)
# Print all the alphabets present in a given string in the specified pattern format
print(
    "The alphabets present in a given string{", gvn_str, "} in the specified pattern:")
print(alphabets)

Output:

The numbers present in a given string{ Python1213prog786rams } in the specified pattern:
['1213', '786']
The alphabets present in a given string{ Python1213prog786rams } in the specified pattern:
['Python', 'prog', 'rams']

Example3

Here it returns two lists. Only the index positions where there is a number are filled with a number in the first list; all other index places are filled with empty characters. The same holds true for the alphabets in the second list.

import re
gvn_str = "Python1213prog786rams"
numbrs = re.findall(r'\d*', gvn_str)
alphabets = re.findall(r'[a-zA-Z]*', gvn_str)
# Print all the numbers present in a given string in the specified pattern format
print(
    "The numbers present in a given string{", gvn_str, "} in the specified pattern:")
print(numbrs)
# Print all the alphabets present in a given string in the specified pattern format
print(
    "The alphabets present in a given string{", gvn_str, "} in the specified pattern:")
print(alphabets)

Output:

The numbers present in a given string{ Python1213prog786rams } in the specified pattern:
['', '', '', '', '', '', '1213', '', '', '', '', '786', '', '', '', '', '']
The alphabets present in a given string{ Python1213prog786rams } in the specified pattern:
['Python', '', '', '', '', 'prog', '', '', '', 'rams', '']