0

My script asks for user interaction and asks for his/her name. I am using raw_input to exhibit this functionality. I want to check whether user gave any input or not. I thought to check the entered string with a blank. Currently the code looks like this :

str = raw_input("Enter your name: ")
if("" in str):
    print "user din't entered anything"

The above code works partially and if user presses enter without any input, the output user din't entered anything is printed.

The issue is that the above code also works when user enters something like foo bar [Notice the space between foo and bar. Yes I know why this is happening.

Another alternative is to check the length of string entered. If user does not enter anything then length would be zero. But same issue arises here. What if user enters more than one blank ! The length logic will fail.

What shall I do to check whether user inputted any string or left it blank ? Is there anything for string in python where I can do if(str == "") Any help appreciated.

4
  • if not str: print 'no input' Commented Jul 2, 2014 at 7:42
  • Thanks for the quick reply but this fails with more than one blank. Commented Jul 2, 2014 at 7:44
  • 1
    @v1h5: Why do you say "more than one blank"? Assuming by "blank" you mean whitespace characters, it treats any amount of whitespace as valid input, not just more than one. Also, '' in x is true for any string x, not just for strings with spaces in them. Commented Jul 2, 2014 at 7:51
  • My bad. I just din't realize that in solving the matter :). +1 for bringing that to notice. Thanks. Commented Jul 2, 2014 at 8:02

2 Answers 2

2
value = raw_input("Enter your name: ")
if not value.strip():
    print "user din't entered anything"

strip() removes the trailling blank spaces and between word . if it is empty spaces . it remove all

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

4 Comments

I think that comment from @v1h5 was written before the logic was fixed.
"and between word" - no it doesn't. It removes leading and trailing whitespace, but not between words.
I entered <blank>foo<blank><bar><blank><blank> and the output says "User din't entered anything" .. :-/
@v1h5 please try with updated one.i tried same i am not getting it
1

isspace() checks if the string is just spaces without needing to create a new string object as strip() would

if(not str or str.isspace()):
    print "user din't entered anything"

Aside: str is a builtin. Shadowing it with your own variables can cause interesting bugs

3 Comments

@user2357112, indeed. That case needs to be checked for
This is working perfectly fine. I am accepting this as answer. cheers @gnibbler
and yes, str is a function in python. I used str just for the sake of example. I will take care not to use this. :)

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.