[英]How do i print the reverse of name in python without using function?
假設我輸入了john doe garry
的input
,我想打印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.