Difference between Py and py3: Input input () function

Source: Internet
Author: User
  • Python 1, 2.7Is generally usedInput ()It is more common, but it can also be usedRaw_input (); They still have the following differences

C: \ windows \ system32> Python
Python 2.7.13 (v2.7.13: a06454b1afa1, Dec 17 2016, 20:42:59) [MSC v.1500 32 bit (
Intel)] on Win32
Type "help", "Copyright", "Credits" or "License" for more information.

>>> S = input ("Enter:") Enter saatraceback (most recent call last): file "<stdin>", line 1, in <module> file "<string>", line 1, in <module> nameerror: Name 'saa 'Is Not Defined

>>> S = input ("Please input :")

Enter 'asxs'

>>> S
'Asxs'

When I useRaw_input ()Function time:

>>> S = raw_input ("input something:") Input something: haha >>> s '\ xb9 \ xfe \ xb9 \ xfe' >>>> S = raw_input ("input something:") Input something: axsx >>> s 'axsx'

>>> S = raw_input ("input something :")
Input something: 0
>>> S
'0'
>>> Type (s)
<Type 'str'>

ForPython2.7OfRaw_input ()Function,For any input, the raw_input () function treats it as a string..

HoweverInput ()No error is reported if you enter data of numbers, lists, and tuples.

>>> s=input("input something:")input something:1234556>>> s1234556>>> type(s)<type ‘int‘>>>> s=input("input something:")input something:(1,2,‘a‘,999)>>> s(1, 2, ‘a‘, 999)>>> type(s)<type ‘tuple‘>>>> s=input("input something:")input something:[1,2,[12,2],42,(2,2,‘a‘)]>>> s[1, 2, [12, 2], 42, (2, 2, ‘a‘)]>>> type(s)<type ‘list‘>
  • HoweverPython3Or even noneRaw_input ()This function

  

HoweverPython3SupportedInput ()Function, however, it can directly accept a string of characters:

>>> S = input ("input something :")
Input something: aaaaa
>>> S
'Aaaaa'
>>> S = input ("input something :")
Input something :( 12,23, 'A ')
>>> S
"(12,23, 'A ')"
>>> Type (s)
<Class 'str'>
>>> S = input ("input something :")
Input something: 123
>>> S
'123'
>>> Type (s)
<Class 'str'>
>>> S = input ("input something :")
Input something: 'asx'
>>> S
"'Asx '"
>>> S = input ("input something :")
Input something: "wsacvd12324qaa"
>>> S
'"Wsacvd12324qaa "'

As you can see,For any input, the input () function of python3 treats it as a string..

  Summary wave,Python2OfRaw_input ()Functions andPython3OfInput ()The function functions are almost the same. It treats user input as the content of the entire string, and the output type is also the STR string type;

  Python3Not SupportedRaw_input ()Function;

  Python2OfInput ()It is a God-like existence. It intelligently identifies user input content and gives corresponding types. when inputting a string in a singular number, you must add 'xxx' "XXX" quotation marks to the string.

Above

Difference between Py and py3: Input input () function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.