Python error TypeError string indices must be integers

0 votes

I am trying to execute following code :

Given a getStr() function, write the necessary sequence of operations to transform the string (containing three literals) in such a way that every literal is tripled? respectively.

def getstr(s) :
    result = 0 
    for i in s :
        result = result + int(s[i] * 3)
    #strlen = len(s)
    return result

print(getstr("abc"))

And it gives me a following error.

TypeError                                 Traceback (most recent call last)
<ipython-input-72-87f4e23e4363> in <module>
      6     return result
      7 
----> 8 print(getstr("abc"))

<ipython-input-72-87f4e23e4363> in getstr(s)
      2     result = 0
      3     for i in s :
----> 4         result = result + int(s[i] * 3)
      5     #strlen = len(s)
      6     return result

TypeError: string indices must be integers

What is the issue ?

Apr 3, 2020 in Python by Falguni
• 140 points

edited Apr 5, 2020 by Falguni 1,537 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.