Detailed explanation of Python's standard input and output

零到壹度
Release: 2018-04-02 15:35:11
Original
4265 people have browsed it


This article shares with you a detailed explanation of Python’s standard input and output. The content is quite good. I hope it can help friends in need

1. Standard input and output

1. Print to the screen

The simplest way to generate output is to use the print statement, which can be separated by zero or more expressions with commas. This function passes the expression into a string and writes the following result to the standard output -

print ("Python is really a great language,", "isn't it?")
Copy after login

This will produce the following result on the standard screen:

Python is really a great language, isn't it?
Copy after login

2. Read keyboard input

There are two built-in functions in Python2 that can read data from the standard input, which defaults to the keyboard. These functions are: input() and raw_input().
But in Python3, the raw_input() function has been deprecated. In addition, the input() function reads data from the keyboard as a string, regardless of whether quotes (" or "") are used.
Example:

x=input("请输入x=")
y=input("请输入y=")
z=x+yprint("x+y="+z)
Copy after login

Running result:

请输入x=111
请输入y=222
x+y=111222
Copy after login

Yes See that the return value of input is always a string. When we need to return int type, we need to use the form of int(input()), for example:

x=int(input("请输入x="))y=int(input("请输入y="))
z=x+yprint("x+y=",z)
Copy after login

The running result is as follows:

请输入x=111
请输入y=222
x+y= 333
Copy after login

3 , Formatted output

Generally speaking, we want to have more control over the output format, rather than simply dividing it with spaces. There are two ways:
The first one is controlled by yourself. String slicing, concatenation operations, and some useful operations included in string
Example:

# 第一种方式:自己控制 for x in range(1, 11): 
  print(str(x).rjust(2), str(x*x).rjust(3), end=' ') 
  print(str(x*x*x).rjust(4))
Copy after login

Output:

 1   1    1
 2   4    8
 3   9   27
 4  16   64
 5  25  125
 6  36  216
 7  49  343
 8  64  512
 9  81  729
10 100 1000
Copy after login

In the first method, str.rjust( of the string object. ) method is to move the string to the right and fill it with spaces on the left by default. The length is specified by the parameters. Similar methods include str.ljust() and str.center(). These methods do not write anything. , they only return a new string, and they do not truncate the string if the input is long
The second is to use the str.format() method
Usage: It passes {}#. ## and : instead of the traditional % way

  • Use positional parameters

Key points: It can be seen from the following example that positional parameters are not subject to order constraints and can be {}, as long as there is a corresponding parameter value in the format. The parameter index starts from 0. The incoming positional parameter list can be in the form of * list ##. #
>>> li = ['hoho',18]
>>> 'my name is {} ,age {}'.format('hoho',18)
'my name is hoho ,age 18'
>>> 'my name is {1} ,age {0}'.format(10,'hoho')
'my name is hoho ,age 10'
>>> 'my name is {1} ,age {0} {1}'.format(10,'hoho')
'my name is hoho ,age 10 hoho'
>>> 'my name is {} ,age {}'.format(*li)
'my name is hoho ,age 18'
Copy after login

    Use keyword parameters
  • Key points: The keyword parameter values ​​must match. You can use a dictionary to pass in the value as a keyword parameter. Before the dictionary Just add **
>>> hash = {'name':'hoho','age':18}
>>> 'my name is {name},age is {age}'.format(name='hoho',age=19)
'my name is hoho,age is 19'
>>> 'my name is {name},age is {age}'.format(**hash)
'my name is hoho,age is 18'
Copy after login

    Filling and formatting
  • Format:
{0:[Fill character][Alignment< ;^>][width]}.format()

<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">&gt;&gt;&gt; &amp;#39;{0:*&gt;10}&amp;#39;.format(20) ##右对齐 &amp;#39;********20&amp;#39; &gt;&gt;&gt; &amp;#39;{0:*&lt;10}&amp;#39;.format(20) ##左对齐 &amp;#39;20********&amp;#39; &gt;&gt;&gt; &amp;#39;{0:*^10}&amp;#39;.format(20) ##居中对齐 &amp;#39;****20****&amp;#39;</pre><div class="contentsignin">Copy after login</div></div>

    Precision and base
  • >>> &#39;{0:.2f}&#39;.format(1/3)
    &#39;0.33&#39;
    >>> &#39;{0:b}&#39;.format(10)    #二进制
    &#39;1010&#39;
    >>> &#39;{0:o}&#39;.format(10)     #八进制
    &#39;12&#39;
    >>> &#39;{0:x}&#39;.format(10)     #16进制
    &#39;a&#39;
    >>> &#39;{:,}&#39;.format(12369132698)  #千分位格式化
    &#39;12,369,132,698&#39;
    Copy after login
    Use index
  • >>> li
    [&#39;hoho&#39;, 18]
    >>> &#39;name is {0[0]} age is {0[1]}&#39;.format(li)
    &#39;name is hoho age is 18
    Copy after login

    The above is the detailed content of Detailed explanation of Python's standard input and output. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template