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

This community-built FAQ covers the “Strings and Conditionals (Part Two)” exercise from the lesson “Introduction to Strings”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Computer Science

FAQs on the exercise Strings and Conditionals (Part Two)

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

3 posts were split to a new topic: I can just use return?

19 posts were split to a new topic: How to not get duplicate letters?

2 posts were split to a new topic: Used a brute force method; is it a bit messy?

2 posts were split to a new topic: Why doesn’t my loop work?

2 posts were split to a new topic: Why does my code return with {} instead of []?

8 posts were split to a new topic: If I use in do I need to set something to return True?

7 posts were merged into an existing topic: How to not get duplicate letters?

A post was merged into an existing topic: How to not get duplicate letters?

This is another of many exercises that wants us to use knowledge we have not been given.
We are being told that using in we can check wether its True or False that one string is contained in another.
So, this method returns True or False. We were NOT being told how to use in method to gain acces to contents of string. I can tell you if banana and cream have the same letters but how can I append the common part to a list?

2 Likes

The explanation given in the narrative is sufficient enough to describe membership which is what the in operator is designed to seek.

There are two ways to use in:

  1. as an extraction tool: for x in range(10); and,
  2. as a membership check: if x in y.

In the case of #1, we could print all the values from 0 to 9, and in the case of #2, we could print the outcome which will be True if x is in y, else False.

print ('a' in 'banana')    #  True

We will have by this point done some work with loops, in particular the example above,

for x in range(10):

or,

for letter in word:

Inside the loop we need to make comparisons and when there is a match, to append the matching character to a list.

matches = []
for x in str_a:
    if x in str_b: matches.append(x)
return matches

Now if we don’t wish to allow duplicates, then we need a conditional inside the if statement (or some added logic)…

    if x in str_b:
        if x not in matches: matches.append(x)

or,

    if x in str_b and x not in matches:
3 Likes

You’d try each letter from one of them, so iterate through one.
For each such letter, you would compare it to each of the letters in the other string, so you would iterate through that as well.
And if it is found in the other string, then you’ve found that it exists in both. You can now append it to your result.

What’s new in that? Take a moment to consider how you would do it manually, you would end up with what I just described wouldn’t you? Don’t look for some magic solution to solve the whole thing for you, break it down into smaller parts and solve each individual subproblem until you’ve implemented the overall thing.

1 Like

Is there some clever method programmers use to break problem into smaller parts? Somthing I could “whiteboard”, understand and then piece all of it together and form a code out of it? I heard about pseudocode but I don’t know if I’m using it correctly since stufff like above gets missed by me.

every-day logic.

pen and paper

give yourself the input and produce the output, observe yourself. what. did. you. do.

your program isn’t going to do it any differently


If you know you’re going to iterate through one of the words, then write the loop for that. You’ve now solved one problem.
Inside that loop more things will happen. Pick something and implement it.

1 Like

You’re not supposed to find a specific piece of code.
You’re supposed to come up with some series of steps whatever that may be, and then write that down in code.

So before you touch any code at all, get yourself a complete idea of what needs to happen. Can you tell another person to carry that out and get the correct result? If you don’t have that idea then what are you writing in code anyway, deciding what to do comes first.
That said, you might decide on a very small part, write it, and then reassess what else needs to be done, maybe you’d write out what you have so far to see whether the thing you wrote had the desired effect, and then you’d think of what needs to be done next.

Of course, it helps if your plan involves things that you know how to use. So for example a list, what can you do with a list, and what things of those would be useful, and then make a plan in terms of those operations.

If you know what the different things you’re using are, then you can make some good guesses to what should be possible to do with them as well, even if you haven’t been introduced to them. Or if such a thing does not exist, then go implement it and then it does exist.

1 Like

def common_letters(string_one,string_two):

trip =

for i in string_one:

for j in string_two:

  if i == j:

    trip.append(j)

    return trip

is this code correct

Are there any duplicates in your trip list? If so, then no, it is not correct as expected by the exercise checker.

Try these two words,

mississippi
missouri

Examine the words and compare the letters.

m i s

would be the expected output of your function.

Test are reporting incorrectly for this when this is valid and correct output.

def contains(big_string, little_string):
    return True if big_string in little_string else False

print(contains("big", "bigger"))
print(contains("big", "fast"))

def common_letters(string_one, string_two):
    return [x for x in string_one if x in string_two]

print(common_letters("abc", "abde"))

A membership test yields a boolean…

return little_string in big_string

Not sure what your trying to say?