Python input/output

Source: Internet
Author: User

1, in python2.x raw_input () and input (), two functions are present, wherein the difference is

    • Raw_input ()---treats all inputs as strings, returning string types

    • Input ()-----can only receive input of "number", has its own attribute when dealing with pure digital input, it returns the type of the number entered (int,float)

2. Raw_input () and input () are integrated in python3.x, removing raw_input (), leaving only the input () function, which receives arbitrary input, defaults all input to string processing, and returns a string type. (examples here are in the PYTHON3 environment)

Example 1:

Name=input ()    # lets the user enter a string of name and save it in the name variable.  Print(' you just entered%s'# shows the string you just entered,   %s is a string escape

Output:

Jeffery, you just entered Jeffery.

Example 2:

Name=input ()        # Let the user enter the name print('Hello', name)    

Output:

Jefferyhello Jeffery

3.Python implements multiple values for one line input

# The string a == = = A + B + cprint(d) is saved in A,b,c

To store integers directly in a,b,c:a,b,c = map (Int,input (). Split ())

4.Str.format () function to format output values

Str.format () can make the output more diverse

Example:

 forXinchRange (1, 3):    Print('{0:2d} {1:3d} {2:4d}'. Format (x, X*x, x*x*x))?Print('{} URL: ' {}! ''. Format ('Baidu','www.baidu.com'))Print('{1} URL: ' {0}! ''. Format ('Baidu','www.baidu.com'))

Output:

1   1   4    8"www.baidu.com! "  " Baidu! " "

The basic use of Str.format () is as follows:

The parentheses and the characters inside them (called formatted fields) are replaced by the parameters in format ().

The number in parentheses is used to point to the position of the incoming object in format ().

Python input/output

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.