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)

5th May 2019, 8:27 AM
R Sahu
3 Answers
+ 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 đŸ€™đŸ€™
5th May 2019, 8:58 AM
Mohd Abdul Sameer
Mohd Abdul Sameer - avatar
+ 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.
5th May 2019, 8:45 AM
Anna
Anna - avatar
+ 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
5th May 2019, 9:16 AM
David Ashton
David Ashton - avatar