Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
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
1 answer
0 votes
1 answer
Welcome to Intellipaat Community. Get your technical queries answered by top developers!

28.4k questions

29.7k answers


94.7k users

Browse Categories