
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Remove All Digits Before Given Number in Python
In this article, we will learn how to remove all digits before a given number in python.
Methods Used
The following are the various methods to accomplish this task ?
Using List Comprehension, split(), enumerate() & index() Functions
Using re module
Using slicing, index() & replace() functions
Example
Assume we have taken an input string and an input number. We will now remove all the digits before the input number from an input string.
Input
inputString = 'hello 6 8 10 tutorialspoint 15 python codes' inputNumber = 10
Output
Resultant string after removing digits before input number{ 10 }: hello 10 tutorialspoint 15 python codes
In the above input string, the digits before the input number 10 are 6, 8. Hence they are removed from the input string and the result string is retained.
Method 1: Using List Comprehension, split(), enumerate() & index() Functions
The enumerate() method adds a counter to an iterable and returns the enumerate object.
Syntax
enumerate(iterable, start=0)
Parameters
iterable ? It can be any sequence/object/iterable supporting iteration.
start ? enumerate() begins counting from this value. If start is not specified, the value 0 is used.
index() function ? The position at the first occurrence of the provided value is returned by the index() function.
The join() is a string function in Python that is used to join elements of a sequence that are separated by a string separator. This function connects sequence elements to convert to a string.
Algorithm (Steps)
Following are the Algorithm/steps to be followed to perform the desired task -.
Create a variable to store the input string.
Print the input list.
Create another variable to store the input number.
Use the split() function to split a string into a list of words and get the index of the input number in a string using the index() function.
Iterate in the input String with index and element using the enumerate() function and remove all the digits/numbers before the given number.
Use the join() function to convert the list into a string.
Print the resultant string after removing digits before the input number.
Example
The following program removes all the digits before a given number in an input string using list Comprehension, split(), enumerate() & index() functions -
# 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, "}:\n", resultantStr)
Output
On executing, the above program will generate the following output -
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 re module
Example
The following program removes all the digits before a given number in an input string using re module -
# 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 # Using regex substitution resultantStr = re.sub('[023456789]', '', inputString[0: inputString.index( str(inputNumber))]) + inputString[inputString.index(str(inputNumber)):] # printing resultant string after removing digits before input number print("Resultant string after removing digits before input number{", inputNumber, "}:\n", resultantStr)
Output
On executing, the above program will generate the following output -
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 slicing, index() & replace() functions
replace() function ? returns a copy of the string that replaces all occurrences of an old substring with another new substring.
Syntax
string.replace(old, new, count)
Algorithm (Steps)
Following are the Algorithm/steps to be followed to perform the desired task -
Convert the input number into a string using str() function and get the index of the input number in an input string using the index() function.
Initialize a variable to store all the digits as a string.
slicing the string after the input number
Use the for loop, to traverse through each character of the above digits.
replace the current digit with a space/blank using replace() function.
Print the resultant string after removing digits before the input number.
Example
The following program removes all the digits before a given number in an input string using slicing, index(), and replace() functions -
# 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 secd_str = inputString[num_index:] # traversing through each character of digits for c in digits_str: # replacing the first string with a null character 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, "}:\n", (first_str+secd_str))
Output
On executing, the above program will generate the following output -
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
Conclusion
This article taught us three different methods for removing all the digits preceding the given number. Using the enumerate() function, we have also learned how to iterate with index and element value.