09 Python input and output format beautification format method

Read keyboard input

Python provides the input() built-in function to read a line of text from standard input. The default standard input is the keyboard.

input can receive a Python expression as input and return the result of the operation.

Instance

str = input("Please input:");
print ("What you input is: ", str)

This will produce the following results corresponding to the input:

Please enter: 123
What you entered is: 123

Output format beautification

Python has two ways of outputting values: expression statement and print() function.

The third way is to use the write() method of the file object. The standard output file can be referenced with sys.stdout.

If you want more diverse output forms, you can use the str.format() function to format the output value.

If you want to convert the output value into a string, you can use the repr() or str() function to achieve.

str(): The function returns a user-readable expression.
repr(): Produce an interpreter-readable expression.

>>> s = 'Hello, Runoob'
>>> str(s)
'Hello, Runoob'
>>> repr(s)
"'Hello, Runoob'"
>>> str(1/7)
'0.14285714285714285'
>>> x = 10 * 3.25
>>> y = 200 * 200
>>> s = 'x 的值为: ' + repr(x) + ',  y 的值为:' + repr(y) + '...'
>>> print(s)
x 的值为: 32.5,  y 的值为:40000...
>>> #  repr() 函数可以转义字符串中的特殊字符
... hello = 'hello, runoob\n'
>>> hellos = repr(hello)
>>> print(hellos)
'hello, runoob\n'
>>> # repr() 的参数可以是 Python 的任何对象
... repr((x, y, ('Google', 'Runoob')))
"(32.5, 40000, ('Google', 'Runoob'))"

Format method introduction

Insert picture description here
If you add a serial number to the slot, you can also specify the matching of the elements in the format
Insert picture description here

The corresponding formatting configuration method inside the slot is as follows:

Be sure to remember the format of the format {<parameter number>: <format control tag>}

Insert picture description here
Examples:
Insert picture description hereInsert picture description here

Guess you like

Origin blog.csdn.net/bj_zhb/article/details/104735139