Is there a way to use .isdigit() with strings that could contain negative numbers? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Is there a way to use .isdigit() with strings that could contain negative numbers?

Would the best way to do this be to check for the "-" at the start of the string, strip it and then reapply it after the .isdigit() check? This seems a bit convoluted.

26th Jan 2017, 1:49 AM
syn
syn - avatar
2 Answers
+ 6
use lstrip() function first. This will work for positive and negative numbers. str = "-2137" result = str.lstrip('-').isdigit()
26th Jan 2017, 9:40 AM
Vlad
Vlad - avatar
0
Thanks!
26th Jan 2017, 4:35 PM
syn
syn - avatar