# FAQ: Introduction to Strings - Strings and Conditionals (Part Two)

Just for fun, I tried to do this without using ‘in’ and had a good time! Wanted to share!

big_string = “watermelon”
little_string = “melon”

#writing a ‘contains’ function without using ‘in.’ Doing this by checking big_string for every instance of little_string’s first letter, and then slicing a section of big_string starting from that letter of length len(little_string). Each of these are segments of big_string are saved into a list of strings called possible_matches.

def contains(big_string, little_string):
possible_matches =
for item in range(len(big_string)):
if big_string[item] == little_string:
possible_matches.append(big_string[(item):(item+len(little_string))])

#A new loop is initiated to compare every item in possible_matches to little_string

check = 0
for item in possible_matches:
if item == little_string:
check+=1
return check
if check == 0:
return False
else:
return True

Once I have defined “contains” or “common letters” and their function, how do I test it out? How can I get it to return T/F based on input? Thanks for your help!

def contains(big_string, little_string):
return little_string in big_string

def common_letters(string_one, string_two):
common =
for letter in string_one:
if (letter in string_two) and not (letter in common):
common.append(letter)
return common

Does this code actually print anything in the terminal for anyone else?

I tried adding a print(common) and getting traceback: common not defined…

The variable `common` is in function scope and cannot be accessed after the return from the function. Either print the call expression itself, or assign the return to a variable, then print that one.

``````print (func(a, b))

c = func(a, b)
print (c)
``````
1 Like

cool! yes the print (func(a, b)) worked!

Thank you

1 Like

Not sure what the issue is. I am getting the right answer they are asking for, but its telling me I’m not.

I finally got this to work,
def common_letters(string_one, string_two):
z =
for x in string_one:
for y in string_two:
if x == y and x not in z:
z.append(x)
return z

I think order matters, so you can’t use a set (because a set may rearrange the order).

1 Like

I couldn’t agree more.

My code is below. It is working:

``````def contains(big_string, little_string):
return (little_string in big_string)

print(contains("watermelon", "berry"))

def common_letters(string_one, string_two):
temp = []
for character in string_one:
if character in string_two:
temp.append(character)
return list(dict.fromkeys(temp))

print(common_letters('manhattan', 'san francisco'))

print(common_letters("banana", "cream"))
``````

i used a brute force method but it works:

def common_letters(one,two): count = [] for i in one: for j in two: if i == j : if i in count: continue count.append(i) return count print(common_letters("manhattan","san francisco"))

a more optimised method would be as followed:

def common_letter(one,two): common =[] for i in one: if(i in two ): if(i in common): continue common.append(i) return common print(common_letter("manhattan","san francisco"))

Hey, there! I am having problem with my code!

def common_letters(string_one, string_two):
wanted =
i = 0
for i in range(len(string_one) - 1):
if string_one[i] in string_two == True:
wanted.append(string_one[i])
i += 1
return wanted

Here’s my code, but it wouldn’t return anything. Can anyone find out why?
This is a problem from ‘Introduction to Strings - Strings and Conditionals (Part Two)’

Thanks all!

Please format your code by selecting the gear icon and “preformatted text”

Or,

1 Like
``````def common_letters(string_one, string_two):
wanted = []
i = 0
for i in range(len(string_one) - 1):
if string_one[i] in string_two == True:
wanted.append(string_one[i])
i += 1
return wanted

``````

I did it now. Thanks for letting me know!

2 Likes

There are a number of issues to consider:

• Your loop doesn’t iterate over the last character of `string_one`.
``````# You wrote:
for i in range(len(string_one) - 1):

# It should be:
for i in range(len(string_one)):
``````

You can confirm this by placing a print statement (for debugging purposes) `print(string_one[i])` as the first statement of your loop. You will see that your loop doesn’t access the last character of the string.
`len(string_one)-1` is the last index of the string, so that’s fine.
But, `range` doesn’t include the `stop` value i.e.

``````print(list(range(5)))
# [0, 1, 2, 3, 4]
# The stop value 5 is not included in the range.
``````

Therefore,

``````for i in range(len(string_one)):
``````

will iterate over the index of the last character without going out of bounds, whereas

``````for i in range(len(string_one) - 1):
``````

will not iterate over the index of the last character of the string.

I don’t know whether it has been covered in the course at this point, or whether it is a later lesson, but if the index is not necessary and you just want to iterate over the characters of a string, then you could use a loop like:

``````word = "Hello"
for c in word:
print(c)
``````
• Since you are using a `for` loop to iterate over the range, so the statements `i = 0` and `i += 1` don’t serve any purpose and should be removed. If you were using a `while` loop, then both the statements would be needed for proper initialization and to avoid infinite loop. In a `for` loop, the initialization and increment are handled by the loop itself.

``````# You wrote:
if string_one[i] in string_two == True:

# It should be:
if string_one[i] in string_two:
``````

You could write the condition as

``````# Valid syntax
if (string_one[i] in string_two) == True:
#
# but it is better to write as:
if string_one[i] in string_two :
``````

The way that you wrote your condition (since the `in` and `==` have equal precedence in Python), your condition would be interpreted as:

``````if string_one[i] in string_two == True:
#
# is equivalent to:
if (string_one[i] in string_two) and (string_two == True):
``````

Hence, your condition is not evaluating to `True` and nothing is being appended to `wanted`.

• Also, the exercise mentions

The letters in the returned list should be unique. For example,
common_letters(“banana”, “cream”) should return [‘a’].

So, you will probably want to modify your condition a little to handle this. You may want to have your `if` condition also check whether the matched character is already present in the `wanted` list or not.

Hi guys, I will copy one of your looping solutions. Thank you.
I used sets in my code but CodeAcademy did not like it … I think it did not like the fact that the returned order of characters in the list is different … a bit silly to be honest …

Actually, I quite like my code. It is short. Uses no loops or conditionals. It terms of pure performance it might be slower, possibly. But who cares about performance using interpreted Python anyway … I think the implemented check by Code Academy is somewhat wrong. The order of returned letters should not matter. At least it is not stated in the question.
Enjoy coding.

def common_letters(string_one, string_two): common = [] for letter in string_one: if (letter in string_two) and not (letter in common): common.append(letter) return common print(common_letters("Antarctica", "Africa and Australia"))

Why does the letter ‘t’ get appended to the new list when using the solution provided in ‘View Solution’?

Your Codebyte produces the following output:

``````print(common_letters("Antarctica", "Africa and Australia"))

# Output: ['A', 'n', 't', 'a', 'r', 'c', 'i']
``````

The `"t"` is in the list because it is present in both strings `"Antarctica"` and `"...Australia"`

If that is not the issue your are talking about, then elaborate more on what you find confusing.

No, I’m an idiot haha. My eyes completely missed the ‘t’ in Australia