0

I want to get the output if the input value type is "Int" then print this message "please enter the string not integer" else calling the function.

It seems Python automatically takes the value type as "String". But I want to get the message like I mentioned if enter type. Please check the below code.

def strlength (string):
    lengthstring = len(string)
    return (lengthstring)

string = input("enter the string: ")
if type(string) == int:
    print("please enter the string not integer")
else:
    print(strlength(string))

Thank you in advance

  • Possible duplicate of [How to check if string input is a number?](https://stackoverflow.com/questions/5424716/how-to-check-if-string-input-is-a-number) – mkrieger1 Aug 05 '18 at 19:43

2 Answers2

1

It seems Python automatically takes the value type as "String"

No. The value type is string, until you convert it to something else. There is no secret magic that guesses possible variable types from the user's input.

There is a helper function isdigit you can use to check if a string only consists of digits.

response = input("enter the string: ")

if str.isdigit(response):
    ...
Tomalak
  • 332,285
  • 67
  • 532
  • 628
1

this is your solution

 def strlength (string):
    lengthstring = len(string)
    return (lengthstring)

string = input("enter the string: ")

try:
    string = int(string)
    print("please enter the string not integer")
except :
    print(strlength(string))
Shofiullah Babor
  • 412
  • 3
  • 10