Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (47.6k points)

I have an application that has a couple of commands. When you type a certain command, you have to type in additional info about something/someone. Now that info has to be strictly an integer or a string, depending on the situation.

However, whatever you type into Python using raw_input() actually is a string, no matter what, so more specifically, how would I shortly and without try...except see if a variable is made of digits or characters?

1 Answer

0 votes
by (106k points)

You can use the below-mentioned code to check if a variable is an integer or a string:-

def check_int(s): 

if s[0] in ('-', '+'): 

return s[1:].isdigit() 

return s.isdigit()

Related questions

0 votes
1 answer
0 votes
1 answer
+1 vote
2 answers
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...