Getting error string indices must be integers

I tried trying my approach but not getting the result required and keep getting the error: “String indices must be integers”

def common_letters(string_one, string_two):

  #List to hold common characters

  str=[]

  for char in string_one:

    #comparing if character exist in both

    if(string_one[char] in string_two):

      #Adding common character in str[]

      str.append(char)

  return str

** https://www.codecademy.com/courses/learn-python-3/lessons/introduction-to-strings/exercises/strings-in-conditionals-ii**

These are two separate functions def contains and def common_letters. Which one are you having an issue with?

1 Like

Sorry must have pasted all.

Referring to the deff common_letters

Based on the error code it sounds like you’ve attempted to index a string using another string. Like lists indexing by element (character) for a string should be integer numbers-

name = "bob"
print(name[1])
Out: o

Have a look in your code for any instances where you might’ve passed a type you didn’t mean to.

For future posts could you please format the code- How do I format code in my posts? as it’s much easier for others to read :+1:.

4 Likes

wow thanks, solved!!

1 Like