+ 2
Check if String is an Integer
Hey! I saw that there is a special method to check if a string is an integer: .isdigit() but I canât use it. Do I have to import a special library for that? It seems like the interpreter doesnât know that method...
3 Answers
+ 7
It should work without modules. Try the following:
st = "123"
bool = st.isdigit()
print(bool)
if bool:
print("runs")
+ 8
def RepresentsInt(s):
try:
int(s)
return True
except ValueError:
return False
>>> print RepresentsInt("+123")
True
>>> print RepresentsInt("10.0")
False
+ 3
@Pedro Demingos Thanks! It works. I hope it will in my project too