iT邦幫忙

0

Python 新手問題 字串輸出問題

如果輸入 hello 時
output 結果會是 hhehelhellhelloellolloloo

請問python coding該怎樣寫?

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 個回答

2
微笑
iT邦研究生 5 級 ‧ 2020-12-23 17:59:00
最佳解答
#存放字串
value = input()
#迴圈起始值
i = 1
#前字串
aStr = ""
#後字串
bStr = ""
#當 i 比 輸入字串長度還大時跳出
while i<len(value):
  aStr += value[:i]
  bStr += value[i:]
  i=i+1
  
#輸出結果
print(aStr+value+bStr)
0
froce
iT邦大師 1 級 ‧ 2020-12-23 21:15:25

挑戰行數盡量少而已。最佳解請給別人

s = "hello"

s1, s2 = zip(*[(s[0:i], s[len(s)-i:]) for i in range(1, len(s))])
print("".join(s1)+s+"".join(s2[::-1]))

我要發表回答

立即登入回答