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!

How am i supposed to know from the introduction on this lesson that i can just type : return letter in word?? Also i have no idea how :

Write a function called common_letters that takes two arguments, string_one and string_two and then returns a list with all of the letters they have in common.

Is equal to:
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

I am so so so lost. I dont understand loops at all and now strings are killing me. I feel like I am too stupid to understand the lessons.

3 Likes

Did you not read the white column with the exercise-specific guide on the left? Not to mention, what you wrote works perfectly well, so I think you’re doing fine.

(Don’t mind me, I’m just here to complain about the compiler throwing TabErrors and IndentationErrors at me left and right.)

1 Like

Hi, for the second part of this exercise it seems to makes no relation to string_two under the function common_letters. How is that so?
deeper explanation about the whole part 2 of this exercise is much appreciated.

2 Likes

We only need to iterate over one of the strings since we are not looking for uniques in each string, which would mean iterating both strings. We are looking for letters in common.

Given a letter (or character) from the one string, we use in to see if it is in the other string. If not, go to the next letter. If it does, append the letter to the return list only if it is NOT present already.

1 Like

I don’t understand how not (letter in common) works in this function.

common =
for letter in string_one:
if (letter in string_two) and not (letter in common)

For example, when letter is break down as b, a,n,a,n,a in the (letter in string_one), and in the (letter in string_two), it tries to match up the letter, and will produce [a,a,a]. But I think common has a empty set, and how could it act to remove the duplicate? Or I want to say that how the variable common works in this case. Please advice.

1 Like

not (letter in common) will be True if letter is not found in the common list. The same can be said of this…

 letter not in common

which does not require brackets. NOT will negate a boolean, so not True will be False, and not False will be True.

1 Like

Thank you. Very helpful.

1 Like

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

You are not the only one. The “and not” really threw me off. I don’t think it was ever mentioned before. It’s good to know what it means now though.

So what does it mean? Describe it for us. We can make this a learning moment.

Oh I just didn’t know we could use “and not” to combine two conditions in an if statement. If it was mentioned in the Hint section or somewhere in the IF lesson, that’d be great. Thanks for your prompt response!

Understood. Let’s walk back a little to where we only know about if and nothing about and or not.

if condition:
    # condtion is truthy

if is like an operator that works on its argument. Given any object or expression it will behave the same. Evaluate this in terms of truthiness. Nothing that isn’t truthy will get past this point and will be shunted off to the next claus (elif, else) or the program statement following.

Logic is all about truth factors. There are only two, True and, False. That’s it. Every object can be boiled down to one of these two. Call them cases if you will; I do for variation in thinking about logic.

it is going to rain tonight

Is that truthy, or falsy? Other factors may come into play. Are there clouds on the horizon? Is it forty below zero outside (only thing reining (pun) is cold)?

The above may well prove out, but it is only an assertion until then.

it rained last night

Is that truthy or falsy? Assuming it did rain, we would take it as truthy.

if it rained last night:
    the assertion was correct

But in cases where there is doubt, we always need another avenue of testing assertions.

Logical operators are a tool we use for this purpose (among others, perhaps).

if prediction of rain AND temperature suitable AND cloudy:
    expect rain

Logic is about judgement and determination as opposed to imperative.

if prediction:
    expect rain

AND is an operator that short-circuits if it encounters a falsy. The whole thing just shuts down and hands back, False.

That’s the end of the evaluation. This tells us that if we have condition that is likely to fail, then put it last and let the others have a chance first, to succeed if they may.

x = []
if len(x) and x[0]

This will not raise an exception because the doable operation came first. The second operand will never be evaluated.

x = []
if x[0] and len(x)

Will toss up an exception so quick your hands won’t have left the keys yet.

The operator is not blame here. The programmer is.

x = [1]
if not x[0] and len(x)

What will that do?

Or this,

if len(x) and not x[0]

Bottom line, go down this rabbit hole and don’t come back up until you are dreaming about logical operators in your sleep, literally.

Arm yourself with simple truth tables for each of AND, OR, NOT.

Bear in mind that AND and OR are operators whereas NOT is a modifier. Operators are binary, modifiers are unary. They prefix and sometimes suffix a singular adjacent object.

not a

First off, whatever a evaluates as, truthy or falsy will become a boolean. The next thing to happen will be toggling that boolean. Given we have only two factors, that is simple enough. True becomes False; False becomes True. Hardly any evaluation process here. It is imperative. But the evaluation of a was determinative. We had to squeeze it to see what came out.

2 Likes

Wow thank you!!

I’m not sure if you work for Codecademy. If you do, do you know if the company will:

  • update the content for the Python courses where the prerequisite is Python 2? Do those courses use Python 2 instead of Python 3?
  • launch more Python courses for Pro membership in the future?
1 Like

Not CC staff; we are volunteers on this side. Contact your advisor or someone on the support team for reliable information with regard to upcoming courses. All we know is that Python 2 will be sunsetting next year, but the content here will remain for a good long time afterward Whether or not any deficiencies will be remedied, one cannot say.

3 Likes

oh god me too, im feeling so dumb for not understanding this… i have to look at almost all the anwers, im feeling very disappointed

in this one im having trouble in here :

if (letter in string_two) and not (letter in common):
common.append(letter)

-why the: " and not letter in common" ?

  • also, why is the first part relevant, i deleted and got the same answer?

thank you

not (letter in common) will be True if letter is not found in the common list. We don’t want any duplicate items in the common list, hence checking first, before appending.

3 Likes

Hi guys. I’m stuck on an exercise. The instruction is:

Write a function called common_letters that takes two arguments, string_one and string_two and then returns a list with all of the letters they have in common.

I require for my code to only return one set of identical letters, not duplicates.

def common_letters(string_one, string_two):
  common = []
  for i in string_one:
    if i in string_two:
      common.append(i)
  return common
      
print(common_letters('manhattan', 'san francisco'))

#Output in the terminal 
['a', 'n', 'a', 'a', 'n']

#Required output in terminal
['a','n']

Solved with the following code:

def common_letters(string_one, string_two):
  common = []
  for i in string_one:
    print(i)
    if i in string_two:
      if not i in common:
        common.append(i)
  return common

hello,
need to know the mistake in this code
def contains(big_string, little_string):
for character in big_string:
if character == little_string:
return True
and when i look at the solution i don’t understand the answer of the first Q