-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. ???

3
  • 4
    Your input cannot be anything but a string. Commented Nov 4, 2020 at 8:46
  • 3
    Does this answer your question? How to find out if a Python object is a string? Commented Nov 4, 2020 at 8:46
  • 1
    Welcome to Stackoverflow! When asking your question, try to format your code properly, using correct indentation, especially in Python where indents matter Commented Nov 4, 2020 at 8:53

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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))

Comments

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...")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.