Easy Tutorial
❮ Java Inner Class Summary Java Printf Formate Demo ❯

The differences between raw_input() and input() in Python 2.x and Python 3.x

Category Programming Techniques

  1. In Python 2.x, both raw_input() and input() functions exist, with the following differences:
  1. In Python 3.x, rawinput() and input() have been integrated, rawinput() has been removed, and only the input() function is retained, which accepts any input, processes all input as strings by default, and returns a string type.

For example:

Python 2.3.4 (#1, Feb 2 2005, 11:44:13) 
[GCC 3.4.3 20041212 (Red Hat 3.4.3-9.EL4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> user=raw_input("please input:")       
please input:wei                          # raw_input successfully inputs a string
>>> user
'wei'
>>> user=input("please input:")          
please input:123                          # input successfully inputs a number (returns the number)
>>> user
123
>>> user=raw_input("please input:")
please input:111               # raw_input successfully inputs a number (still treated as a string)
>>> user
'111'
>>> user=input("please input:")
please input:wei                          # input fails to input a string
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "<string>", line 0, in ?
NameError: name 'wei' is not defined

In Python 3.2.3, input and raw_input have been integrated, and raw_input no longer exists:

Python 3.2.3 (default, Apr 11 2012, 07:15:24) [MSC v.1500 32 bit (Intel)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>> user=raw_input("please input:")                 # raw_input no longer exists
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'raw_input' is not defined
>>> user=input("please input:")
please input:wei
>>> user
'wei'
>>> user=input("please input:")                     # the output of input is always treated as a string
please input:123
>>> user
'123'

>

Original article: https://blog.csdn.net/suibianshen2012/article/details/51378948

#

-

** qykqlr

* qyk**@163.com

In Python 2.x, input() is not limited to receiving numbers only.

In Python 2.x, input() is equivalent to eval(rawinput(prompt)), used to obtain console input. rawinput() treats all input as a string and returns a string type. In contrast, input() has its own characteristics when dealing with pure numeric input, returning the type of the entered number (int, float).

Note: Both input() and raw_input() can accept strings, but raw_input() directly reads console input (it can accept any type of input). As for input(), it expects to read a valid Python expression, meaning that when you enter a string, you must enclose it in quotes, otherwise it will raise a SyntaxError.

Unless there is a specific need for input(), we generally recommend using raw_input() for user interaction.

Note: In Python 3, the default type received by input() is str.

** qykqlr

* qyk**@163.com

** Click here to share my notes

Cancel

-

-

-

❮ Java Inner Class Summary Java Printf Formate Demo ❯