FAQ: Code Challenge: String Methods - Check Name

This community-built FAQ covers the “Check Name” exercise from the lesson “Code Challenge: String Methods”.

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

Learn Python 3

FAQs on the exercise Check Name

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

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!

2 posts were split to a new topic: Does .lowercase work on uppercase?

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

A post was split to a new topic: My solution

def check_for_name(sentence, name):
  for i in sentence.split():
    if i.lower() == name.lower():
      return True
  for y in sentence.split():
    if sentence.find(name) == -1:
      return False

This was my solution. Not at all streamlined like the solution provided by CC.

I think its a good challenge to see if you can simplify your solution

Yea, you’re right. The solution given by CC was much better than mine

How can

[spoiler]This text will be blurred[/spoiler]
return name.lower() in sentence.lower() 

return True or False?
I thought it will return name.lower()

[spoiler]This text will be blurred[/spoiler]
def check_for_name(sentence, name):
  return name.lower() in sentence.lower()

The in keyword is used for a membership test and will result in a boolean.

4 Likes

Thank you. Now I understand. :smiley:

1 Like

Screen Shot 2020-03-27 at 4.04.16 PM
instead of finding the Trues one by one, I tried to find one False and use an else statement after that. maybe not the best wa.

I wrote a code that was not as streamlined as CC’s but resulted in expected returns. Yet, it didn’t seem to be read as correct by CC to unlock next task. Is there any way to see what is wrong with my code?

You return strings, you should return Boolean values

return strings is risky, lets say you want to use the function for a comparison. Then "False" is a truthy value. Let me show you what i mean:

if "False":
   print("This is true")
else:
   print("This is false")

if we we where to use a Boolean value instead, we would get This is false

so, i recommend returning a boolean value.

1 Like

OK, now it makes more sense. Did not think about that. Thank you!

How does grouping ‘or’ ‘and’ etc. work compared to grouping in normal algebra.

Specifically, my question is: why is this -

if name in sentence_upper or name in sentence_lower or name in sentence_title or name in sentence_mix

different from this -

if name in (sentence_upper or sentence_lower or sentence_title or sentence_mix)

Thank you!

edit: I also realise you don’t need all these conversions. checking against one conversion is enough!

In the second example the grouping will result in a boolean.

if name in True:

That should tell us something. Notice in the first example each operand is a separate boolean. From left to right, the first one that evaluates to True will be the one that short-circuits the expression giving a True result.

Thank you for the help so far!

I understand to some extent…
but how can the part in parentheses be evaluated?
it doesn’t make sense

There is no True or False to the statement
a or b or c or d…

My follow-up question would be: is there a method of contracting my first code then?

Oh, but there is… They have a truth value known as, truthiness which opposite is falsyness.

Since we can assume that all the operands are non-empty strings, which are truthy. the evaluation will end on the first operand and yield True for the whole expression.

 name in sentence_upper
|-------A boolean------|

 sentence_upper
|truthy or falsy|

Short-circuiting

T or T or T or T
 \
  first True short-circuits

The latter three operands are not evaluated.

F and F and F and F
 \
  first False short-circuits

Understand that operands are expressions, not statements. All expressions boil down to a value. Even literals are technically expressions.

0
1
''
'a'

ans so on, though we seldom refer to them as expressions and usually call them values.

a + b
b - a
a < b
a == b

All the above yield a value, which is what the term, evaluate means.

a or b
a and b

Also expressions that yield a value upon evaluation. First the operand on the left is evaluated, and if truthy (for or) that will be the term yielded. Only if it is falsy is the second operand evaluated, if necessary, else the term yielded. When the first term of an AND expression if falsy, that becomes the yielded term. if truthy then the second term is the yield.

The above does not apply when written in if or while statements. The terms are not yielded, but a boolean result.

Your short-circuiting analogy is great! Thank you and I hope to remember this principle better now.

but my other query remains … so is “sentence_upper” True or False?
Are you saying that if “sentence_upper” is not empty then it is by default True?

1 Like

By default it is truthy, which evaluates to True in an if statement. Logical operators work on the truth values of the operands.

If you have an interactive shell open,

>>> 1 or 2
1
>>> 2 or 1
2
>>> 0 or 1    <= no short-circuit
1
>>> 1 or 0
1