How to print the digits present in a string?(ex:- abc123 gives 123) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How to print the digits present in a string?(ex:- abc123 gives 123)

14th Nov 2019, 1:45 PM
Ayush Tripathi
Ayush Tripathi - avatar
3 Answers
+ 4
If you like it, you can also do it in a comprehension: a = 'a1b2c3d4' print(*[i for i in a if i.isdigit()], sep='') # output: 1234 # or in a for loop: for i in a: if i.isdigit(): print(i, end='')
14th Nov 2019, 3:18 PM
Lothar
Lothar - avatar
+ 3
One line: print("".join([i for i in "abc123" if i.isdigit()]))
14th Nov 2019, 3:55 PM
Asman-H
Asman-H - avatar