[Python Note 1] Input and Output: python Input and Output

Source: Internet
Author: User

[Python Note 1] Input and Output: python Input and Output

1. Input

Python input mainly includes two functions: input and raw_input. What is the difference between them?

Run the code

name=input('please input somthing :')print 'What you input is : '+name



Ps: anyone familiar with C ++ knows that when we define a variable, we need to define its variable type before the variable, but here we define a name variable without specifying its type, this is also a feature of pythond. python dynamically determines the type based on the value assigned to the variable. It is similar to the var type in C, of course, the input type here is of course a string.


Run the following code:

name=raw_input('please input somthing :')print 'What you input is : '+name

We will find the difference between the two, because input will assume that the user inputs a valid python expression. If we input hello, an error will be prompted, but if we input it as a string, the program will run perfectly ('hello ')

However, it is difficult to remember to input a string every time. Therefore, raw_input solves this problem. raw_input regards all input data as raw data and puts it into the string (name) medium.

Therefore, if there are no special requirements for input, we should try to use raw_input. (Raw_inout is renamed as input after python3.0)

No matter whether it is input or raw_input, the strings in the brackets are displayed as the prompt information. Of course, they can also be empty so that no prompt information is displayed.

2. Output

Print is used for python output. It seems that print has been encapsulated into a function after python3, and I am currently using 2.7, so I can continue to use the code similar to the following for output.

print 'hello python'

Ps: anyone familiar with C ++ knows that double quotation marks are used in C ++ to represent strings. What is double quotation marks used in python?

The answer is that in python, single quotes and double quotation marks have the same function and both represent a string.

However, when single or double quotation marks are required in a string, in addition to escape characters (\), the combination of single and double quotation marks is also very convenient.

Run the following code:

print "let's go!"print 'I said :"you are best!"'


In addition, it is reasonable to use three single quotes in python. What functions will be implemented?

Run the code

print '''hell0 ,my name is ER_Plough,nice to meet you!'''


So when we need to wrap our output, it will reflect his role!

Of course, this is also possible.

print 'hell0 ,\nmy name is ER_Plough,\nnice to meet you!'


Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.