+ 9

Can you solve this python trick

this is Python code X=('123abc') how we can print only digits in this string and we assume that we don't know the positions of the digits we know this is alnum statement but how can we print only digits

9th Feb 2018, 11:56 AM
Maninder $ingh
Maninder $ingh - avatar
7 Answers
+ 13
Google : RegEx. #this thing is really overpowered...
9th Feb 2018, 11:58 AM
Valen.H. ~
Valen.H. ~ - avatar
+ 7
x = "123abc" y = list(filter(lambda x: x.isdigit(), x)) print( "".join(y) )
9th Feb 2018, 2:15 PM
SebastiƔn Zapata
SebastiƔn Zapata - avatar
+ 4
this will work! x = '123abc' #every thing in printable will be printed only printable = '1234567890' for ch in x: if ch not in printable: x = x.replace(ch, "") print(x)
9th Feb 2018, 2:21 PM
ʒred
ʒred - avatar
+ 4
x='123abc' print(*[i for i in x if i.isdigit()],sep='')
9th Feb 2018, 3:21 PM
Louis
Louis - avatar
+ 3
Nice answer @Fred
9th Feb 2018, 3:48 PM
DeroG
DeroG - avatar
+ 3
Using the isdigit() method. for i in X: if i.isdigit (): print (i)
2nd Apr 2018, 6:29 AM
Mitali
Mitali - avatar
+ 2
@fred,is it(ur answer )python?
17th Jul 2018, 1:06 PM
Shelly Kapoor
Shelly Kapoor - avatar