Notes on Computer Level 2 Python Brushing Questions---Basic Operation Questions 4 and 20 (examine the output in reverse order)

Article Directory

fourth question

Topic:
insert image description here
Analysis:

  • The first space is similar to the first question,ls = jieba.lcut(txt), returns a slice list of the input text, assigned to ls.
  • Next we need to output the input text in reverse order.
    ls[::-1] is already reversed, we just need to output it.
    But be careful: it is wrong to print(i) directly, because the result of outputting once in a loop will be:
    Mom
    loves
    me
  • soprint(i,end=“”), each time the loop ends with empty, then the output will be connected.

Answer:

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

import jieba
txt = input("请输入一段中文文本:")
ls = jieba.lcut(txt)
for i in ls[::-1]:
    print(i,end="")

operation result:
insert image description here

Question 20

topic:
insert image description here

insert image description here

Parse:

  • The first empty string is printed in reverse order print(s[::-1],end=“”).
    s[::-1] If nothing is filled on the left and right sides of the first colon, it means to search from the beginning to the end, and the second colon represents the step size. Here, the step size is set to -1, then it is from the back to the front to achieve reverse order output .
    end="" means let empty end, then the output will be continuous, which meets the requirement of the number of output strings immediately after the title.
  • Number of second empty strings:only

Answer:

s = input()
print(s[::-1],end="")
print(len(s))

operation result:

insert image description here

おすすめ

転載: blog.csdn.net/weixin_47296493/article/details/130324247