簡體   English   中英

我如何在不使用函數的情況下在python中打印名稱的相反名稱?

[英]How do i print the reverse of name in python without using function?

假設我輸入了john doe garryinput ,我想打印garry doe john

我已經嘗試過很多次了:

for x in range(0,l-1):
    first  = first+' '+li[i][0]
    i-=1
first=li[l-1]+' '+first
print(first)

但這並沒有使我獲得預期的輸出

您可以嘗試以下操作:分割輸入,使用[::-1]片反轉標記,然后join它們重新連接在一起。

input = 'john doe garry'
rev = ' '.join(input.split()[::-1])
print(rev)
# garry doe john

大家好,您的代碼看起來有點改進

na = input('enter your name ')
li=na.split()
li = na.split()
l=len(li)
i=0

for x in range(0,l):
   first=first+' '+li[i-1]
   k-=1
first=first
print(first)

如果您的輸入是john doe garry則輸出是。

garry doe john我希望它對您有用。

如果您只想print

first = input()
for i in first.split()[::-1]:
    print(i, end=' ')
 doegarryjohn(xenial)vash@localhost:~/python/stack_overflow$ python3.7 reverse.py john garry doe doe garry john 

如果要將其存儲為變量:

first = input()
f_reverse = ''
for i in first.split()[::-1]:
        f_reverse += (i + ' ')

print(f_reverse)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM