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

The test expects the results to be in the same order in which they occur in the strings. 'y' comes first, followed by 'o', and then 'n'.

In your second attempt you are not affecting the order. Not sure why you have the expressions in the subscripts.

Consider reading the items directly from the string since we won’t be changing anything.

for i in string_one:
    for j in string_two:

That will simplify things considerably.

Furthermore, do we really need to use nested loops? We can always take a letter from string_one and test for membership in string_two.

if i in string_two and i not in common:

The test expects the results to be in the same order in which they occur in the strings

I can see that, I think it should make that clear (which I thought I made clear).

In your second attempt you are not affecting the order. Not sure why you have the expressions in the subscripts.

I have expressions in subscripts for the express purpose of affecting the order.

Furthermore, do we really need to use nested loops? We can always take a letter from string_one and test for membership in string_two.

I guess I haven’t made it clear that my code is an example of how this test fails people who haven’t done anything incorrect (they might just be testing themselves on negative indices for example), rather than an attempt at “normal” code.

Maybe this was the wrong place to post mistakes - happy to be pointed to a better one. If this isn’t considered a mistake that would also be useful to know (although it reduces my confidence in this resource greatly). Thanks for taking the time anyway.

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

How do you know when parentheses are needed or not. 

for letter in string_one:
if (letter in string_two) and not (letter in common): #<— For this line of code in particular.


1 Like

You can use parentheses to change the order of operations but they are also sometimes used just for the sake of readability. Generally I think it’s best to avoid adding them in unless you either need to change the order of operations or they make the expression much more readable (which can be a little subjective).

If you’re unsure about the order of operations in general then you’d want to look into operator precedence which defines the order in which operations are performed in an expression and for this problem in particular comparisons too.

Hey Sir,
My code is supposed to work fine. I have used the nested loops. Can you explain why does it shows wrong? My another IDE PyCharm is producing right result.

def common_letters(string_one, string_two):
common =
for i in string_one:
for a in string_two:
if i == a:
common.append(i)
return common

Without indentation this one is not clear. Please format the code- How do I format code in my posts? so others can read it.

Task 2 in this lesson does state that the recorded common_letters should be unique. Try a few different strings to see if your code can actually meet that requirement.

1 Like

Can the in keyword be used for List and Tuples as well (since they are all ordered collections)?

Hello, @javarockstar24562, and welcome to the forums.

Why not try it, and see?

If you don’t yet have Python installed on your PC, you could try it here.

Short Answer:

Yes.

2 Likes

Thank you. Got it right using nested loops.

This exercise really makes me re-think programming. I feel like its so early to feel this stuck, especially after looking at the “solution” im like “dam should I have known that already?” …
viewing the solutions in previous exercise I felt " oh I should’ve known that" however for this question it feels like I vaguely remember setting an empty list.
I wish the hints had reminded me of that. instead of .append. I totally felt lost.
Ill continue to finis the content but getting stumped like that makes me think im not learning properly or efficiently

1 Like

Hi guys, I found a short code for the answer to this exercise, but it doesn’t work :slightly_frowning_face: Can anyone find the mistake please? I’d rather ammend this code (as I understand it), than come up with a totally different one. Thanks.

def common_letters(string_one, string_two):

  lst = []

  for i in string_one:

    if i in string_two:

      if i in lst == False:

       lst.append(i)

  return lst

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