Q:

Removal all characters from a string except integers using python programming

belongs to collection: Python String Exercises

0

Removal all characters from a string except integers

Given:

str1 = 'I am 25 years and 10 months old'

Expected Output:

2510

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Hint:

Use the string function isdigit()

Solution:

str1 = 'I am 25 years and 10 months old'
print("Original string is", str1)

# Retain Numbers in String
# Using list comprehension + join() + isdigit()
res = "".join([item for item in str1 if item.isdigit()])

print(res)

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python String Exercises

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Write a python program to find words with both alp... >>
<< Remove special symbols / punctuation from a string...