方法1
s = input()
new_s = ""
for i in s:
if 97 <= ord(i.lower()) <= 122:
new_s += i
else:
new_s += " "
arr = new_s.split()
print(" ".join(arr[::-1]))
方法2
a = input()
for i in a:
if not 97 <= ord(i.lower()) <= 122:
a = a.replace(i, " ")
b = a.split()
print(*b[::-1])
方法3
s = input().strip() + " "
word = ''
word_list = []
for c in s:
if c.isalpha():
word += c
else:
word_list.append(word)
word = ''
print(' '.join(word_list[::-1]))
注意:使用split()而不是split(' '),因为如果两字母间存在多个空格时,空格也会被分割出来,而题目要求只允许出现一个空格。

京公网安备 11010502036488号