5

I am having trouble understanding the meaning of the following code:

begin = None 
while begin != "": 
    begin = (raw_input("\nBegin:"))

What does begin !="" mean? What does the empty string "" represent?

4
  • 1
    It tests whether the string has nothing in it. Commented Mar 12, 2015 at 4:53
  • variable begin not would be an empty string. Please read the docs, it's a basic question. Commented Mar 12, 2015 at 4:53
  • If the user just presses Return in response to the prompt, it will be set to the empty string. Commented Mar 12, 2015 at 4:54
  • Also note that that code is very unpythonic. You would conventionally write while not begin: to test for the empty string Commented Mar 12, 2015 at 5:00

2 Answers 2

10

What does begin !="" means?

It means begin does not refer to an empty string -- it's satisfied at the start (as begin refers to None, not to an empty string) and will stay satisfied as long as the user types anything else than just a <return>.

What does the empty string "" represent??

It represents what raw_input returns when the user just hits the <return> (AKA <enter>) key without actually typing anything at the prompt.

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

Comments

3

"" represents an empty string. So begin != "" is true whenever begin doesn't contain an empty string. The initial value None is not an empty string, so the loop will run at least once. After that, begin will contain whatever the user entered in response to the Begin: prompt. If he enters nothing (i.e. just presses Return) it will be an empty string, the test will fail, and the loop will end.

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.