0

I was under the impression that in Python it was necessary to write (str) when converting an int to a str, but in this case, if the user inputs a number, it still executes fine

print('Enter a name.')
name = input()
if name:
    print('Thank you for entering a name, ' + name)
else:
    print('You did not enter a name.')

I didn't need to write + str(name) on line 4 when the input entered was 1, for example (or any number).

JustAkid
  • 109
  • 1
  • 1
  • 8
  • Because you are using Python 3 in which `input()` always returns the string type object (unlike Python 2.x) – Moinuddin Quadri Jan 26 '18 at 21:38
  • So if this were python2, which we still use at work, what would I have to do differently? – JustAkid Jan 26 '18 at 21:40
  • In Python 2, pretty much forget about `input()`. Use `raw_input()`, which is basically the same as `input()` in Python 3. – Fred Larson Jan 26 '18 at 21:41
  • 1
    And if you are interested in writing your code independent of the Python version,you may simply do `str(name)` with `input()`. There is no harm in type-casting it again – Moinuddin Quadri Jan 26 '18 at 21:42
  • 2
    @MatthewH yes, the Python 2 input works differently, it is equivalent to `eval(raw_input())` which is just... dangerous. Use `raw_input` always, pretty much. – juanpa.arrivillaga Jan 26 '18 at 21:45
  • 1
    @MoinuddinQuadri no harm at all: https://stackoverflow.com/questions/42223125/should-i-avoid-converting-to-a-string-if-a-value-is-already-a-string (or use `format` instead) – Jean-François Fabre Jan 26 '18 at 21:45
  • ok guys, I appreciate all the answers. I'll post more questions as they come, this was sufficient. Have a great one – JustAkid Jan 26 '18 at 21:49
  • Be careful to check the [official documentation](https://docs.python.org/3/) before asking here. – Jongware Jan 26 '18 at 22:04

3 Answers3

1

If you read the docs on input then you will see this line that refers to your scenario:

The function reads a line from input, converts it to a string (stripping a trailing newline), and returns that.

We can also see this in the interpreter:

>>> type(input())

<class 'str'>

And, to clarify, Python will never return an int from input(), you always need to convert if you want to do arithmetic operations or otherwise.


Note that the behaviour is different in Python 2 which offers input() and raw_input(). The first of which will convert the input to the appropriate data type (such as int) whereas the latter will function the same as input() in Python 3.

This can again be tested in the interpreter:

>>> type(input())
5
<type 'int'>
>>> type(raw_input())
5
<type 'str'>
Joe Iddon
  • 20,101
  • 7
  • 33
  • 54
0

Because Python does some stuff in the background to figure out what the variable types are. Plus, the input from input is always a string. You just concatenated two strings.

SuperStew
  • 2,857
  • 2
  • 15
  • 27
  • Oh, I see. input is always a string. – JustAkid Jan 26 '18 at 21:39
  • Because when the user enters `5`, Python gets `'5'`. This is just how `input` works. Whatever you type will be a string to python until you convert it to something else. – SuperStew Jan 26 '18 at 21:40
0

The function input always returns a string. What you just witnessed is a string concatenation.

If you wanted to convert the results of input into a different type, say integers, then you can do:

x = int(input("Enter a number"))

This will raise an error if the input cannot be expressed as an int type though. You can do additional parsing or type testing (e.g. isinstance() or type() is...) depending on the complexity of the input you're expecting.

r.ook
  • 13,466
  • 2
  • 22
  • 39