[Python note 1] input and output

Source: Internet
Author: User

1. Input

Python input mainly has input and raw_input two functions, what is the difference between these two?

Run the code, please.

Name=input (' Please input somthing: ') print ' What are input is: ' +name



PS: Everyone familiar with C + + knows that when we define a variable we need to define its variable type before the variable, and here we define a name variable, but we don't specify its type, which is also a feature of Pythond, Python implements dynamic deterministic type, The type is determined based on the assignment of the variable, which is somewhat similar to the Var type in C #, and of course the type entered here is, of course, a string


Run the following code

Name=raw_input (' Please input somthing: ') print ' What are input is: ' +name

We will find the difference between the two, because input assumes that the user is entering a valid Python expression, and if we enter hello in knowledge, it will prompt an error, but if we enter it as a string, the program will run perfectly (' Hello ')

But it's hard to remember the input string every time, so raw_input solves the problem, and Raw_input takes all the input as raw data and puts it into the string (name).

So if there are no special requirements for input, we should use raw_input as much as possible.

Either input or raw_input the string inside the parentheses is displayed as a hint, and of course it can be empty, so that no hint message is displayed

2. Output

The Python output uses print, as if the print has been encapsulated as a function after Python3, and I'm using 2.7 so I can continue to output using code similar to the following

print ' Hello python '

PS: People familiar with C + + know that in C + + they use double quotes to represent strings, and what are double quotes in python for?

The answer is that in Python, the function of single and double quotes is the same, representing a string.

But when we need to have single or double quotes in the string, the combination of single and double quotes is also very convenient in addition to using the escape character (\).

Run the following code

Print "Let ' s go!" print ' I said: "You're best!"


In addition, the use of three single quotes in Python is also reasonable, what will achieve what function?

Run the code, please.

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


So when our output is going to need a newline, that's what he does!

Of course, it's also possible.

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


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

[Python note 1] input and output

Related Article

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.