- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to remove characters except digits from string in Python?
In this article, we are going to find out how to remove characters except digits from a string in Python
The first approach is by using the if statement in a for loop and joining them using join() method. We will iterate the string using the for loop and then we will check whether each character is a digit or not using if statement, if it is a digit then we will go to next character, otherwise we will replace that character.
For iterating repeatedly through a sequence, use a for loop. This functions more like an iterator method seen in other object-oriented programming languages and is less like the for keyword found in other programming languages. The for loop allows us to run a series of instructions once for each element of a list, tuple, set, etc.
Example
In the example given below, we are taking a string as input and we are removing every character except digits using for loop and if statement −
str1 = "W3lc0m3" print("The given string is:") print(str1) print("Removing all the characters except digits") print(''.join(i for i in str1 if i.isdigit()))
Output
The output of the above example is given below −
The given string is: W3lc0m3 Removing all the characters except digits 303
using filter() and lambda()
The second approach is by using filter() and lambda(). We will iterate over the string using the lambda() function and we will filter out the characters that are not digits using the filter() method and print the output string with only digits removing all other characters.
Example
In the example given below, we are taking a string as input and we are removing every character except digits using filter() and lambda() and printing the resultant string. −
str1 = "W3lc0m3" print("The given string is:") print(str1) print("Removing all the characters except digits") print(list(filter(lambda i: i.isdigit(), str1)))
Output
The output of the above example is as shown below −
The given string is: W3lc0m3 Removing all the characters except digits ['3', '0', '3']
Using Regular Expressions
Regular expressions are used in the second technique. Import the re library and install it if it isn't already installed to use it. After importing the re library, we can use the regular expression "\D". We'll use the term "\D" to represent all characters other than numbers in the re.sub function, and we'll replace the characters that are not digit with empty spaces.
Example
In the example given below, we are taking a string as input and we are removing all the characters other than digits using the regular expressions and print the resultant string −
import re str1 = "W3lc0m3" print("The given string is:") print(str1) print("Removing all the characters except digits") print(re.sub("\D", "", str1))
Output
The output of the above example is given below −
The given string is: W3lc0m3 Removing all the characters except digits 303