-1

I want to check input is an equal to string or not. But, my code cannot see if statement. I think the problem is in if statement equation:

    name = input("Name: ")
    if name != str:
        print("Please enter letter answer ...")
        name = str(input("Name: "))
    else:
        print(input(name))             

I guess I cannot write name != str. But I don't know how to check input is equal to string. ???

  • 4
    Your input cannot be anything but a string. – khelwood Nov 04 '20 at 08:46
  • 3
    Does this answer your question? [How to find out if a Python object is a string?](https://stackoverflow.com/questions/1303243/how-to-find-out-if-a-python-object-is-a-string) – funie200 Nov 04 '20 at 08:46
  • 1
    Welcome to Stackoverflow! When asking your question, try to format your code properly, using correct indentation, especially in Python where indents matter –  Nov 04 '20 at 08:53

4 Answers4

0

First of all, you can't check an input string against a type str. inputs will always be of type str. If you want to check for strings in general, you can use type(var) == str.

0

input will always return a string, so you don't have to check if it is a string.

Also, this line: print(input(name)) asks for input again, you probably just want print(name)

This code should work just fine for what you would want:

name = input("Name: ")
print(name)

If you want the name to not include any numbers or spaces, so it is just a single name, you can try isalpha():

if name.isalpha():
    pass # Do your stuff with the name here
funie200
  • 3,688
  • 5
  • 21
  • 34
0

I would recommend using isinstance(object, type) function because it is a boolean function already for example: if isinstance(name,str):

you can also use the type() function if you want to use an approach more like what you are already using. The type() function can be useful overall for example:

if type(name) != str:
   print("Error; name is ",type(name))
Ponyboy
  • 76
  • 5
0

In Python, Whatever you enter as input, the input() function converts it into a string. If you enter an integer value, still it will convert it into a string.

Using isaplha() like @funie200 said:

while True:
    name = input("Name: ")

    if name.isalpha():
        print(name.title)
        break
    else:
        print("Please enter your name...")