我想在所有方向上打印一个单词,假设我得到单词“牛顿”,结果应该是:
NEWTON
E
W
T
O
NOTWEN
O
T
W
E
NEWTON我所做的如下所示,但我如何才能让用户只输入一次单词,比方说输入一次,然后得到结果呢?在我的例子中,我是一个接一个地打印它们。谢谢
>>> from itertools import zip_longest
>>> text = "newton".upper()
for x in zip_longest(*text.split(), fillvalue=' '):
print (' '.join(x))
def reverse(phrase):
return ' '.join(list(map(lambda x: x[::-1], phrase.split())))
print(reverse("newton").upper())
char = 'newton'.upper()
cont = len(char) - 1
while cont >= 0:
cont2 = char[cont]
print(cont2)
cont -= 1
print("newton".upper())``` 发布于 2021-05-09 09:18:01
您可以使用sep和end参数来简化切片。
def print_e_shape(s):
print(s)
print(*s[1:], sep='\n', end='')
print(s[-2::-1])
print(*s[-2::-1], sep='\n', end='')
print(s[1:])
print_e_shape('NEWTON')输出
NEWTON
E
W
T
O
NOTWEN
O
T
W
E
NEWTONhttps://stackoverflow.com/questions/67453472
复制相似问题