Python Program to Remove all digits before given Number

In this article, we will learn how to remove all digits before a given number from a string in Python. This task is useful when processing text data where you need to clean up numeric content selectively.

Problem Overview

Given an input string containing words and numbers, we want to remove all standalone digits that appear before a specific target number, while keeping the target number and everything after it intact.

Example

Consider this input ?

inputString = 'hello 6 8 10 tutorialspoint 15 python codes'
inputNumber = 10

The expected output ?

Resultant string after removing digits before input number{ 10 }:
hello 10 tutorialspoint 15 python codes

In this example, the digits 6 and 8 appear before 10, so they are removed from the input string.

Method 1: Using List Comprehension with split() and enumerate()

This approach splits the string into words, finds the target number's position, and filters out digits that appear before it ?

# input string
inputString = 'hello 6 8 10 tutorialspoint 15 python codes'

# printing input string
print("Input String:", inputString)

# input number
inputNumber = 10

# splitting a string into a list of words and
# getting the index of input number in a string
num_index = inputString.split().index(str(inputNumber))

# Getting the result using list comprehension
resultantList = [element for p, element in enumerate(inputString.split()) 
                 if not (p < num_index and element.isdigit())]

# converting the list into a string using join() function
resultantStr = ' '.join(resultantList)

# printing resultant string after removing digits before input number
print("Resultant string after removing digits before input number{", inputNumber, "}:")
print(resultantStr)
Input String: hello 6 8 10 tutorialspoint 15 python codes
Resultant string after removing digits before input number{ 10 }:
hello 10 tutorialspoint 15 python codes

Method 2: Using Regular Expressions

This method uses the re module to substitute digits with empty strings in the portion before the target number ?

# importing re(regex) module
import re

# input string
inputString = 'hello 6 8 10 tutorialspoint 15 python codes'

# printing input string
print("Input String:", inputString)

# input number
inputNumber = 10

# Find the position of the target number
target_pos = inputString.index(str(inputNumber))

# Using regex substitution to remove standalone digits before target
before_target = inputString[:target_pos]
after_target = inputString[target_pos:]

# Remove standalone digits (digits surrounded by spaces or at boundaries)
before_cleaned = re.sub(r'\b\d+\b\s*', '', before_target)
resultantStr = before_cleaned + after_target

# printing resultant string after removing digits before input number
print("Resultant string after removing digits before input number{", inputNumber, "}:")
print(resultantStr)
Input String: hello 6 8 10 tutorialspoint 15 python codes
Resultant string after removing digits before input number{ 10 }:
hello 10 tutorialspoint 15 python codes

Method 3: Using String Slicing and replace()

This approach slices the string at the target number's position and removes all digit characters from the portion before it ?

# input string
inputString = 'hello 6 8 10 tutorialspoint 15 python codes'

# printing input string
print("Input String:", inputString)

# input number
inputNumber = 10

# getting the index of input number in a string
num_index = inputString.index(str(inputNumber))

# storing all the digits in a string
digits_str = "0123456789"

# slicing the string till before the input number
first_str = inputString[:num_index]

# slicing the string after input number
second_str = inputString[num_index:]

# traversing through each character of digits
for c in digits_str:
    # replacing each digit with empty string
    first_str = first_str.replace(c, "")

# printing resultant string after removing digits before input number
print("Resultant string after removing digits before input number{", inputNumber, "}:")
print(first_str + second_str)
Input String: hello 6 8 10 tutorialspoint 15 python codes
Resultant string after removing digits before input number{ 10 }:
hello  10 tutorialspoint 15 python codes

Comparison of Methods

Method Approach Best For
List Comprehension Word-based filtering Preserving word boundaries
Regular Expressions Pattern matching Complex text processing
String Replacement Character-by-character removal Simple digit removal

Conclusion

We explored three different methods to remove digits before a given number in a string. The list comprehension approach is most suitable for preserving word boundaries, while the regex method offers more flexibility for complex patterns.

---
Updated on: 2026-03-27T00:01:48+05:30

378 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements