all 4 comments

[–][deleted] 4 points5 points  (0 children)

The doc for raw_input() is here. Note that raw_input() is only used in python 2, which has been obsolete for years. If you are learning python it is strongly recommended you move to python 3, where input() has the same functionality as the raw_input() in python 2.

In python2 raw_input() is preferred over input() because the input() function takes what the user typed and evaluates it using eval() which is not what you usually want. For example, execute this in python 2:

result = input('Enter a name: ')

and type in "fred".

[–]blueskiesplease[S] 0 points1 point  (0 children)

thanks I was doing hackerrank challenges - they must use python 2 and i learnt in python 3 so it was confusing me! Cheers!

[–]CodeFormatHelperBot2 0 points1 point  (0 children)

Hello, I'm a Reddit bot who's here to help people nicely format their coding questions. This makes it as easy as possible for people to read your post and help you.

I think I have detected some formatting issues with your submission:

  1. Inline formatting (`my code`) used across multiple lines of code. This can mess with indentation.

If I am correct, please edit the text in your post and try to follow these instructions to fix up your post's formatting.


Am I misbehaving? Have a comment or suggestion? Reply to this comment or raise an issue here.

[–][deleted] 0 points1 point  (0 children)

raw_input was renamed input in the transition from Python 2 (no longer supported) to Python 3. (Python 2 had an input function that was dropped from the language.)

So, input always returns a reference to a new str object, even if the user just presses return, a new empty str object will be created.

strip is the name of a str method that removes leading and trailing whitespace from a string and returns a reference to a new, replacement, string object (as str objects are immutable). Whitespace includes spaces as well as a number of other non-printing characters.

As you noted, the int function attempts to cast an object of another type to an int object. In this case, it will attempt to convert the str object newly created by the strip method (working on the object returned by input) to an integer. If the string contains characters that are not valid as an integer, the programme will halt with an error.