Home >Backend Development >Python Tutorial >Detailed explanation of Python's standard input and output
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?")
This will produce the following result on the standard screen:
Python is really a great language, isn't it?
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)
Running result:
请输入x=111 请输入y=222 x+y=111222
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)
The running result is as follows:
请输入x=111 请输入y=222 x+y= 333
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))
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
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
>>> 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'
>>> 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'
<pre class="brush:php;toolbar:false;">>>> &#39;{0:*>10}&#39;.format(20) ##右对齐
&#39;********20&#39;
>>> &#39;{0:*<10}&#39;.format(20) ##左对齐
&#39;20********&#39;
>>> &#39;{0:*^10}&#39;.format(20) ##居中对齐
&#39;****20****&#39;</pre>
>>> '{0:.2f}'.format(1/3) '0.33' >>> '{0:b}'.format(10) #二进制 '1010' >>> '{0:o}'.format(10) #八进制 '12' >>> '{0:x}'.format(10) #16进制 'a' >>> '{:,}'.format(12369132698) #千分位格式化 '12,369,132,698'
>>> li ['hoho', 18] >>> 'name is {0[0]} age is {0[1]}'.format(li) 'name is hoho age is 18
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!