Output from the Python output python3
The output in Python3 uses the function print (), as shown in the following example:
>>> print(‘hello kitty‘)
Print () can also accept multiple parameters, separated by commas:
>>> print(‘hello‘,‘kitty‘)hello kitty
After you see the string merge output, the middle pattern is separated by commas ~
The print function can receive other data types in addition to receiving strings
>>> print(1) # 接收整数1>>> print(1+2) # 表达式3>>> print([1,2,3]) # 列表[1, 2, 3]>>> print({‘a‘:1,‘b‘:2}) # 字典{‘a‘: 1, ‘b‘: 2}
Output in the Python2
The output in Python2 uses print plus the output data, as shown in the following example:
>>> print ‘hello kitty‘
You can also receive multiple parameters:
>>> print ‘1+2 =‘,31+2 = 3
Usage is basically the same as the print () function in Python3 ~
Python formatted output
Formatting the output string
>>> print(‘My name is %s‘ % (‘abc‘))My name is abc
% represents the format operation, and the%s (format character) in the preceding string is replaced with the string ' abc ' after%.
To print an integer:
>>> print("I‘m %d year old" % 18) # 当只有一个值的时候,可以不适用小括号I‘m 18 year old
Multiple format characters:
>>> print("I‘m %s. I‘m %d year old" % (‘abc‘, 18))I‘m abc. I‘m 18 year old
Multiple format characters can also use dictionaries to pass values:
>>> print("I‘m %(name)s. I‘m %(age)d year old" % {‘name‘:‘abc‘, ‘age‘:18})I‘m abc. I‘m 18 year old
Format character
%s 字符串 (采用str()的显示)%r 字符串 (采用repr()的显示)%c 格式化字符及其ASCII码%b 二进制整数%d 十进制整数%u 格式化无符号整型%o 格式化无符号八进制数%x 格式化无符号十六进制数%X 格式化无符号十六进制数(大写)%e 用科学计数法格式化浮点数%E 作用同%e,用科学计数法格式化浮点数%f 格式化浮点数字,可指定小数点后的精度%g %f和%e的简写%G %f 和 %E 的简写%% 字符"%"
?
The format character reserves the location for the real value and controls the format of the display.
The format can be further controlled in the following ways:
%[(name)][flags][width]. [Precision]typecode
(name) is named
Flags can have--, ' or 0. If you do not write the default means right-justified. -Indicates left alignment. ' is a space that fills a space on the left side of a positive number to align with a negative number. 0 means using 0 padding.
Width indicates display widths
Precision indicates the precision after the decimal point
Examples are as follows:
>>> print("%4d" % 5) # flags不填(默认右对齐),width为4(总长为4位) 5>>> print("%-4d" % 5) # flags为 - ,表示左对齐5>>> print("%06d" % 5) # 总长为6位,flags为0,即左边使用0填充000005>>> print(‘-- %f --‘ % (1.23)) # 格式化浮点数-- 1.230000 -->>> print(‘-- %5.2f --‘ % (1.2345)) # 总长5位,小数点后保留2位-- 1.23 -->>> print(‘-- %05.2f --‘ % (1.2345)) # 总长5位,小数点后保留2位,flags为0,左边使用0填充(小数点也占一位)-- 01.23 --
There is another format for Python, using format, which is also the official recommended way:
方式一:>>> print("My name is {0}. I‘m {1} year old. Hello {0} !!".format(‘baby‘, 18))My name is baby. I‘m 18 year old. Hello baby !!方式二:>>> print("My name is {name}. I‘m {age} year old. Hello {name} !!".format(name=‘baby‘, age=18))My name is baby. I‘m 18 year old. Hello baby !!
Input in Python input Python3
Input in Python3 uses input () to store the user's input in the terminal into a variable
>>> name=input()hello>>> name‘hello‘
Input () can take a parameter as a hint when the user enters the message, as shown in the following example:
>>> name = input("What is your name?")What is your name?abc>>> name‘abc‘
Tip:input () processes the data entered by the user as a string (str) ~
>>> lst = input()[1,2,3,4,5]>>> type(lst)<class ‘str‘>
Input in the Python2
The Raw_input usage in Python2 is similar to input () in Python3:
>>> age = raw_input("How old are you?")How old are you?12>>> type(age)<type ‘str‘>
Tip:raw_input Also, the data entered by the user is treated as a string (str).
The python2 can also use input () to receive input from the user, where the input () usage differs from the input () in the Python3
>>> name = input("What is your name?")What is your name?baby # 这里输入的是 变量 baby,而不是字符串,由于 baby 变量没有定义,所以报错Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<string>", line 1, in <module>NameError: name ‘baby‘ is not defined>>> name = input("What is your name?")What is your name?‘baby‘ # 这里输入的是 字符串 ‘baby‘,成功赋值~>>> lst = input()[1,2,3,4,5] # 输入的是 列表类型,lst变量即为列表~>>> type(lst)<type ‘list‘>
The input () in Tip:python2 is the type that is entered when it receives the data entered by the user, and why it is stored. Note the difference
.................^_^
Python input and output