How to extract only positive integers from a string in Python

1 Answer

0 votes
str = "1php 23 c++ -4 python 5 java 3.14"

l = [int(s) for s in str.split() if s.isdigit()]

print(l)

 
 
   
'''
run:
   
[23, 5]
   
'''

 



answered Aug 1, 2019 by avibootz
...