- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python - Remove all characters except letters and numbers
When it is required to remove all characters except for letters and numbers, regular expressions are used. A regular expression is defined, and the string is subjected to follow this expression.
Below is a demonstration of the same
import re my_string = "python123:, .@! abc" print ("The string is : ") print(my_string) result = re.sub('[\W_]+', '', my_string) print ("The expected string is :") print(result)
The string is : python123:, .@! abc The expected string is : python123abc
The required packages are imported.
A string is defined and is displayed on the console.
A regular expression is defined, and the string is subjected to it.
The result is assigned to a variable.
This is displayed as output on the console.
- C++ Program to Remove all Characters in a String Except Alphabets
- How to remove characters except digits from string in Python?
- Java program to remove all numbers in a string except "1" and "2"?
- Python program to replace all Characters of a List except the given character
- List comprehension and ord() in Python to remove all characters other than alphabets
- How to remove all special characters, punctuation and spaces from a string in Python?
- MySQL query to remove everything except the last 7 characters in column record
- Remove all except the first character of a string in MySQL?
- How to remove all objects except one or few in R?
- Remove Duplicate Letters in C++
- try and except in Python
- How can I remove all elements except the first one using jQuery?
- Remove first two characters of all fields in MySQL?
- try and except in Python Program