0

Hey (bad english) so i am working on a function that is going to check if a string1 is inside another string2. i do belive i have accomplished this, but after that, i am trying to find the index of where the string2 was found in the string1. if the string is not in the string at all it will return (-1). i have tried googling around and looking for asnwers i have seen someone use s.find(), but for somehow can't use it. Therefor i am asking here for some help this is how i have come:

def contains_string(str1, str2):
    if str1 in str2:
       print(str1.find(str2))
    else:
       print("-1")
    return

str1 = Ronaldo
str2 = Ron
str3 = Messi
print(contains_string(str1, str2))
print(contains_string(str1, str3))
#outcome should be:
#3    <- because Ron goes to index 3 in str1
#-1

i think i am really close, but i am not sure what i am missing

0

1 Answer 1

0

Apparently you have a small logic problem. When you use contains_string(str1,str2), you want to see if for example "Ron" is in "Ronaldo'. But you are checking this:

if str1 in str2:

This is basically saying is "Ronaldo" in "Ron" which is false. Thus, you'll only need to change that line

if str2 in str1:

Hope that helps!

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.