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

You might want to look into evaluation order and operator precedence in Python. The expression i in lst == False is not working the way you expect because of the order it gets evaluated in.

You could change the precedence using parentheses or consider the potentially simpler option of not in.

1 Like

Thank you. I have tried parenthesis
if (i in lst) == False
also,
if i not in lst:
Both work perfectly! :grinning:

1 Like

A post was split to a new topic: Already have my money and don’t really care

Hi,
How come I’m getting this as the result of my function:
<function common_letters at 0x7fd51ed16e18>

Most likely I made a mistake, but the editor does not seem to indicate this. It’s more like when SQL saves the output to a location in memory instead of displaying the actual result.

I would like to see the actual output/result in the terminal (far right part of screen with black background) so entered some variables and a print:

string_one = "today"
string_two = "tomorrow"


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)

Thanks,
Mike

It’s this line here, print(common_letters). The common_letters name refers to the function itself (in Python your functions are wrapped in objects which can be passed around).

That particular output style <function common_letters at 0x7fd51ed16e18> is something you’ll see often when printing objects that haven’t been given a nice text representation (you can see the type of the object, it’s name and the 0x... refers to the virtual address in memory that contains this function).

To use a function you probably want to call it. Calling a function is done with parentheses, e.g. func(). If the function takes arguments these should be included within the parentheses, print(3) for example is a call to the print function with an argument of integer 3.

In your example make sure you call the function with the two arguments you need, (something more like this… print(func(a, b))).

1 Like

ok, got it, thanks! Tested, works good.
We covered Multiple Parameters in the Python3 Functions section (sec. 5.7), but not the way you present it - combined with a print on the same row e.g. print(func(a, b))

1 Like

Ah fair enough. You can get away with a lot of syntax on a single line sometime but it might be more readable to assign it first, e.g.

result = func(a, b)
print(result)

It’s one of those things that is situationally dependent so pick whatever you think is more readable.

1 Like

Perhaps a better redesign of the exercise teaching the in and not in operator ought to have been taught first before expecting something more extravagant. I think a better incremental approach that CC could have taken could have done something like this:

The answer for this shows the boolean True for this sub string vs. the boolean false for the next one. Because for example one cloud is a sub string of on Cloud 9 and the second snippet is False because its not not in.

a_string="I'm on cloud nine."
sub_string = "cloud" in a_string
print(sub_string)
a_string="I'm on cloud nine."
sub_string = "cloud" not in a_string
print(sub_string)
1 Like

Is there a better way to do this? I’ve got this code that works, but it just looks a bit messier than what’s come before so I’m assuming I’ve over complicated it.

def common_letters(string_one, string_two):
  common_list = []
  for letter in string_one:
    if letter in string_two:
      if letter in common_list:
        continue
      else:
        common_list.append(letter)
  return common_list

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[0]:
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.