0

in my class we are studying python 2.7. I am using vscode to test the exercises.

exercise 1: read user input and print the length. If the user write exit the program finish.

My code is follow:

myexit=False
while (myexit!=True):
    #read user input
    txt=raw_input("write a string or write exit to go out: ")
    #print the user input string
    print txt
    if (str(txt)=='exit'):
        myexit=True#exit from while
    else:
        print len(txt) #print the string length
print "finish"

when i test the code i get always the length of the string +1

example: if i write foo the output is 4 and no 3. When i write exit i don't go out from the while and the output is 5.

Where i wrong ?

I have missed a module?

Thanks for your help

7
  • I tried same code in Python 2.7 and also in Python 3.7 where raw_input() was renamed to input() and it works without any problem. Commented Feb 27, 2019 at 6:52
  • I've not been able to repro this in REPL. but try using print repr(text) to see what exactly is in the string Commented Feb 27, 2019 at 6:52
  • Thanks for your reply guys, @Doon i have tried print repr(txt) and i get: example i write my and in output i get my\r. why? Commented Feb 27, 2019 at 7:01
  • Hmm \r is a carriage return. What operating system are you running on? and are you running the file via VScode, or via CLI/Termina/CMD. Short answer is that I am guessing you are using Windows.\r\n is standard windows EOL, whereas unix uses\n. raw_input is stripping the the newline, but the carriage return., and I am not sure why Commented Feb 27, 2019 at 7:05
  • 1
    another option would be to use rstrip() to remove the \r from the end of the line. Commented Feb 27, 2019 at 7:10

1 Answer 1

1

I am not sure exactly why this is happening, and I don't have access to a windows machine to test/verify but based on the comments above, it appears that on the version of python you are using that raw_input is only stripping the newline(\n) and not the carriage return(\r). Windows uses \r\n while unix uses \n. When raw input returns the \r is still on the string, hence the extra char. A useful debugging technique at the cli is to use the function repr() on the value to see exactly how it is represented. This is helpful to locate any stray control or invisible chars in strings.

The function rstrip() will remove all whitespace from the right side of the string, which in this case should safely remove the stray \r. It should also be safe if this code is running on a *nix like system as rstrip() will only remove the whitespace if it is present. You can also specify a set of char to strip, so if you would would like to be pedantic, you could use rstrip("\r").

txt=raw_input("write a string or write exit to go out: ").rstrip("\r")

Should fix the issue while still maintaining compatibility on different versions.

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

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.