How to check if a string is positive integer in Python

3 Answers

0 votes
string = '90012'
 
if string.isdigit():
    print('integer')

     
     
'''
run:
 
integer
 
'''

 



answered Jul 15, 2022 by avibootz
edited Jul 15, 2022 by avibootz
0 votes
string = '90012'

print('948432'.isdigit()) 
print('3.14'.isdigit())
print('-5'.isdigit())
    
    
    
'''
run:

True
False
False

'''

 



answered Jul 15, 2022 by avibootz
0 votes
try:
    str = "18"
     
    n = int(str)
     
    print(n >= 0)
 
except ValueError:
    print("Not a number")    
     
    
     
 
'''
run:
 
True
 
'''

 



answered Aug 19, 2022 by avibootz

Related questions

1 answer 271 views
2 answers 335 views
3 answers 281 views
1 answer 214 views
1 answer 189 views
1 answer 193 views
...