- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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 can I eliminate numbers in a string in Python?
You can create an array to keep track of all non digit characters in a string. Then finally join this array using "".join method.
example
my_str = 'qwerty123asdf32' non_digits = [] for c in my_str: if not c.isdigit(): non_digits.append(c) result = ''.join(non_digits) print(result)
Output
This will give the output
qwertyasdf
example
You can also achieve this using a python list comprehension in a single line.
my_str = 'qwerty123asdf32' result = ''.join([c for c in my_str if not c.isdigit()]) print(result)
Output
This will give the output
qwertyasdf
- Related Articles
- How to eliminate repeated lines in a python function?
- How can I format numbers as dollars currency string in JavaScript?
- How can I tell if a string repeats itself in Python?
- How can I convert bytes to a Python string?
- How can I convert a Python tuple to string?
- How do I check if a string has alphabets or numbers in Python?
- How can I get last 4 characters of a string in Python?
- How can we unpack a string of integers to complex numbers in Python?
- How can I reverse a string in Java?
- How can I fill out a Python string with spaces?
- How can I remove the ANSI escape sequences from a string in python?
- How i can replace number with string using Python?
- How to extract numbers from a string in Python?
- How to concatenate a string with numbers in Python?
- How do I verify that a string only contains letters, numbers, underscores and dashes in Python?

Advertisements