Notes on computer level 2 Python brushing questions ------ basic operation questions 2, 3, 8, 13, 16, 19, 22 (examine format formatted output)


This article sorts out examples of formatted output in computer-level Python, and gives detailed analysis and operation results.
insert image description here
Type inside:
x: hexadecimal lowercase

second question

Topic:
insert image description here
Analysis:

  • n = eval(input("Please enter a number:"))
    If the keyboard enters 123, go through input-"123", and then go through eval-123.
  • The chr() function is to input an integer (0-255) and return its corresponding ascii symbol or unicode encoding, for example:
>>> chr(100)
'd'
>>> chr(53)
'5'
  • The ord() function is used to return the ascii value (0-255) or unicode value () of a single character. For example:
>>> ord('d')
100
>>> ord('5')
53
  • print("{format}".format(content))
    format::+^11. From left to right, they are guide symbols, plus sign characters + padding, center-aligned, and a width of 11 characters.
    content:chr(n-1)+chr(n)+chr(n+1). Use chr() to convert the number to Unicode, and use the plus sign to connect n-1, n and n+1.
    So the answer is: print("{ :+^11}”.format(chr(n-1)+chr(n)+chr(n+1)))

Answer:

# 请在______处使用一行代码或表达式替换
# 注意:请不要修改其他已给出代码

n = eval(input("请输入一个数字:"))
print("{:+^11}".format(chr(n-1)+chr(n)+chr(n+1)))

operation result:
insert image description here

third question

Topic:
insert image description here
Analysis:

  • Check out the format here::->20,From left to right, leading symbol, character-fill, right-aligned, width of 20 characters, thousands separator.

Answer:

# 请在______处使用一行代码或表达式替换
# 注意:请不要修改其他已给出代码

n = eval(input("请输入正整数:"))
print("{:->20,}".format(n))

operation result:
insert image description here

Question eight

Topic:
insert image description here
Analysis:

  • Check out the format here::=^20From left to right, leading symbols, characters = padding, center-aligned, and a width of 20 characters.

Answer:

# 请在______处使用一行代码或表达式替换
# 注意:请不要修改其他已给出代码

s = input("请输入一个字符串:")
print("{:=^20}".format(s))

operation result:
insert image description here

Thirteenth question

Topic:
insert image description here
Analysis:

    • Check out the format here::*>15From left to right, leading symbols, characters * padding, right-aligned, and a width of 15 characters.

Answer:

# 请在______处使用一行代码或表达式替换
# 注意:请不要修改其他已给出代码

n = eval(input("请输入正整数:"))
print("{:*>15}".format(n))

operation result:
insert image description here

Question sixteen

topic:

insert image description here
insert image description here
Parse:

  • Check out the format here::=^14From left to right, leading symbols, characters = padding, center-aligned, and a width of 14 characters.
    Answer:
# 请在______处使用一行代码或表达式替换
# 注意:请不要修改其他已给出代码

n = eval(input("请输入正整数:"))
print("{:=^14}".format(n))

operation result:
insert image description here

Question 19

Topic:
insert image description here
insert image description here
Analysis:

  • Format of the first space survey::=>25,From left to right, leading symbols, characters=padding, right-justified, width 25 characters, with thousands separator.
  • The second air is error-prone! ! ! You cannot directly fill in s here, the result of input is a string, you need to use **eval(s)** Convert a string to a number.

Answer:

# 请在______处使用一行代码或表达式替换
# 注意:请不要修改其他已给出代码

s = input()
print("{:=>25,}".format(eval(s)))

operation result:
insert image description here

Question 22 - Advanced

topic:
insert image description here

insert image description here
Parse:

  • Format of the first space survey::"^30xFrom left to right, it is the leading symbol, the character "fill (note that the slash in front is an escape character, use " itself to fill), center alignment, and a width of 25 characters. x: Hexadecimal lowercase.
  • The second blank is the same as Question 19, **eval(s)** Convert a string to a number.

Answer:

s = input()
print("{:\"^30x}".format(eval(s)))

operation result:
insert image description here

Guess you like

Origin blog.csdn.net/weixin_47296493/article/details/130309827