Open In App

Python – Splitting Text and Number in string

Last Updated : 20 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Given a string containing both letters and numbers, the task is to separate the text (letters) and the numbers into two separate outputs. For example, if the input is “abc123”, the output should be “abc” and “123”. Let’s discuss various ways in which we can achieve this in Python.

Using for loop

This method uses a simple for loop to iterate through the string. Each character is checked to determine whether it is a letter or a digit using basic conditional statements.

Python
# Initializing the input string
a = "abc123"

# Initializing variables for text and number
s = ""
n = ""

# Iterating through each character in the string
for c in a:
    if c.isdigit():  # Checking if character is a digit
        n += c  # Adding to number
    else:
        s += c  # Adding to text

print(s, n)

Output
abc 123

Explanation:

  • We initialize two variables, one for storing text and the other for numbers.
  • Each character is checked using isdigit() to decide whether it belongs to the text or the number part.

Using list comprehensions

This method uses list comprehensions by iterating over the string, we create two separate lists: one for the letters and another for the numbers. These are then joined together to form the final outputs.

Python
# Initializing the input string
a = "abc123"

# Splitting the string using list comprehensions
s = "".join([c for c in a if not c.isdigit()])  # Getting letters
n = "".join([c for c in a if c.isdigit()])  # Getting numbers

print(s, n)

Output
abc 123

Explanation:

  • Two separate list comprehensions are used to filter letters and numbers.
  • The join() function is used to combine the filtered characters into strings.

Using regex

Regular expressions (regex) provide a versatile way to split strings. In this method, we use patterns to identify the letter and number components of the string. Regex is particularly useful for handling more complex strings with additional patterns.

Python
import re

# Initializing the input string
a = "abc123"

# Splitting the string using regex
s = re.findall("[a-zA-Z]+", a)[0]  # Extracting letters
n = re.findall("[0-9]+", a)[0]  # Extracting numbers

print(s, n)

Output
abc 123

Explanation:

  • Regular expressions "[a-zA-Z]+" and "[0-9]+" are used to match letters and numbers.
  • This method is effective for more complex patterns but adds overhead due to the regex library.

Using itertools.groupby

groupby() method from the itertools module groups consecutive elements based on a specified condition. Here, we group characters by whether they are digits or not.

Python
from itertools import groupby

# Initializing the input string
a = "abc123"

# Splitting the string using groupby
groups = ["".join(g) for k, g in groupby(a, key=str.isdigit)]  # Grouping by type
s = groups[0]  # Extracting text
n = groups[1]  # Extracting numbers

print(s, n)

Output
abc 123

Explanation:

  • groupby() function groups consecutive characters based on whether they are digits or not.
  • Groups are combined into strings to get the text and number parts.


Next Article
Practice Tags :

Similar Reads