0 votes
1 view
in Python by (47.7k 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 (107k 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

Welcome to Intellipaat Community. Get your technical queries answered by top developers !