-3

I am trying to check a data type in python. I want to use an if statement to check if the data is a string or not. And everytime i input an integer it returns the value as a string.

Here is the code.

inp = input("Input: ")

if type(inp) != str:
    print("You must input a string")
else:
    print("Input is a string")

this is the message it returns.

Input is a string

I want it to print "You must input a string"

Magnum
  • 36
  • 5
SJXD
  • 15
  • 1
  • 6
  • 10
    `input()` always returns a string. You can only manipulate the input after the user has entered the value. – Cow Sep 09 '22 at 07:09
  • what problem you are trying to solve with this check? – Psytho Sep 09 '22 at 07:14
  • @user56700 agree. Or you may define while taking input like : `inp = int(input("Input: "))` – debugger Sep 09 '22 at 07:14
  • @debugger Exactly, you manipulate the inputted value after it's entered, by converting it to an integer. – Cow Sep 09 '22 at 07:16
  • def has_numbers(inputString): return any(char.isdigit() for char in inputString) to check if a digit is in the string. – karel van dongen Sep 09 '22 at 07:17
  • "And everytime i input an integer it returns the value as a string." Yes, because it **is** a string, and **is not** an integer. You **cannot** "input an integer". Pressing the `1` key and then the `enter` key inputs **a string, not an integer**. Strings can have digit symbols in them. They are not special. They are not different from letters, punctuation, emoji or any other text symbols. – Karl Knechtel Sep 17 '22 at 01:10

3 Answers3

1

First of all numbers can also be strings. Strings are anything which is enclosed in double quotes. Anyway if all you need is to get an input and verify that it's not a number you can use:

inp = input("Input: ")

if inp.isdigit():
    print("You must input a string")
else:
    print("Input is a string")

Or if you wish to have a string with no digits in it the condition will go something like this:

inp = input("Input: ")

if any(char.isdigit() for char in inp) :
    print("You must input a string")
else:
    print("Input is a string")
Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
-1

It will always be a string as input() captures the text entered by the user as a string, even if it contains integers.

Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
Magnum
  • 36
  • 5
  • Is there a way to make it not return a string without making it an integer int(inp) – SJXD Sep 09 '22 at 07:17
  • @SJXD No, there isn't. the return type of `input()` is a string. You have to parse that string according to whatever logic you want to impose, but the function returns a string. – joanis Sep 16 '22 at 14:49
-2

The input() function always returns a datatype of string. To check if what you want to check a simple way would be to iterate over the string and if there is no character present then print "You must input a string".

inp = input("Input: ")
for i in inp:
    if i.isalpha():
        print("Input is a string")
        break
else:
    print("You must input a string")

A better solution would be using try except:

inp = input("Input: ")

try:
    x=int(j)
    print("You must input a string")
except:
    print("Input is a string")

this is because int() function throws an error of there is a letter in the string given to it as an input parameter

Woundrite
  • 1
  • 3
  • You should only catch what you expect to fail, in this case `except ValueError` if `int(j)` throws. – ojdo Sep 09 '22 at 07:28
  • 1
    That is true however out of the scope of the question, ideally one should never leave an open except block it should always specify what it is trying to catch as you said. – Woundrite Sep 09 '22 at 07:31