1.通過改變讀入字串的步長為-1完成倒序讀入
n =
input()
print
(n[::-
1])
2.通過迴圈將字串逐個遍歷合併
n =
input()
for i in
range
(len
(n))
:print
(n[len
(n)-
1-i]
,end=
'')
3.直接使用內建reverse方法
n =
input()
l =list
(n)l.reverse(
)print(''
.join(l)
)
4.使用.pop和.insert方法先正序彈出,再通過迴圈改變insert插入位置
n =
input()
l=list
(n)cnt =
len(l)-1
while cnt >0:
x = l.pop(0)
l.insert(cnt,x)
cnt -=
1print(''
.join(l)
)
n =
input()
l =list
(n)l1 =
for i in
range
(len
(n))
: x = l.pop(
)print(''
.join(l1)
)
6.以中間項為基準對稱交換
n =
input()
l=list
(n)if
len(l)==0
orlen
(l)==1:
print
(n)else
: i=
0 x=
len(l)
while i < x/2:
l[i]
, l[x-i-1]
=l[x-i-1]
,l[i]
i+=1print(''
.join(l)
)
倒序輸出字串
public static void main string args system.out.println result public static string revertstring string str char chars str.tochararray int len chars.le...
如何用python倒序輸出字串?
今天在面試乙個實習生的時候,向他提了乙個問題,用你最喜歡的程式語言實現倒序輸出字串你會怎麼做?小夥子說最喜歡c 然後找我要了支筆在紙上寫了十幾行 用的還是遞迴求解的方法 雖然最後是實現了這個小目標,但這過程讓我總感到焦慮 我問小夥子學過python沒有,小夥子靦腆的說沒有學過,因為在大一的時候學了c...
字串倒序輸出
題目 輸入乙個字串,將該串倒序輸出。例如輸入字串 hello 倒序輸出為 olleh str1 str input 請輸入字串 print 輸入的字元是 s str1 str2 定義乙個空串用來接收倒序後的字串 for i in str1 1 對字串進行倒序輸出 str2 join i 使用str2...