0
If entered string is beats and wanted to print sssss tttt aaa....why error
s=input("enter string") for i in range(len(s),0,-1): print(s[i]*i)
3 Réponses
+ 13
len(s) is 5 if s ="beats"
& the last character of string "beats" is 's' with index 4.
therefore the error occurs.
--------------------
Correct code 👇👇
s=input("enter string\n")
for i in range(len(s)-1,0,-1):
print(s[i]*i)
--------------------------------------------------
Copy this code & check the output
--------------------------------------------------
Hope you got the answer 🤙🤙
+ 2
"beats" has five letters, so len('beats') is 5. The last letter in beats has the index 4 though. s[len(s)] will be out of range.
+ 2
This gives you what you want:
s = input("enter string\n")
for i in range(len(s) - 1, -1, -1):
print(s[i] * (i + 1), end=" ")
https://code.sololearn.com/cO586dSLjWFe/?ref=app