FAQ: Code Challenge: String Methods - X Length


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

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

FAQs on the exercise X Length

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!


I was stuck here for a bit using this code:

def x_length_words(sentence, x):
  words = [ ]
  words.append(sentence.split(" "))
  for word in words:
    if len(word) < x:
      return False
  return True

This kept giving me True and True

I couldn’t figure out why it wasn’t right so asked for the solution, which gave me:

def x_length_words(sentence, x):
  words = sentence.split(" ")
  for word in words:
    if len(word) < x:
      return False
  return True

This obviously provided the correct answer of False and True.

I understand why codecademys solution is more concise than my own but can someone explain why these give different outcomes?
Does words = sentence.split(" ") create a list of the words in the sentence or have I got that wrong?



That is exactly what it does. The argument for the split() method (" ") is the separator string that the sentence is split on resulting in a list of words.


Thanks for clarifying that!
If that is the case though shouldn’t both of these give the same output?


Since split() returns a list, we don’t need to define an empty list to append to.

words = sentence.split(' ');


Whenever declaring an empty list, use the [] object, not [ ]. It’s moot here, though.


I read through these comments, and I have the same question as driggzzz. I also created an empty list and appended the slices to it. I under why the solution does what it does, but wouldn’t appending to an empty list yield the same results and setting the variable equal to the list that slice() creates?

I just think the messier code (i. e. creating an empty list first) would at the very least return the same output. Since that’s not the case, can anyone explain why?


These challenges all seem to be giving me problems.

can anyone tell me why this doesn’t work?

def x_length_words(sentence, x):
  words = []
  for word in words:
    if len(word) in words >= x:
      return True
  return False


Is this a bit redundant?

Can an integer be found in an array of strings?



I am going to go back and revisit some of the older string lessons. I am getting types messed up in every challenge and its not until someone points out how silly it looks that I understand.


I don’t really understand the solution. The first section of code is the answer provided.

def x_length_words(sentence, x):
words = sentence.split(" ")
for word in words:
if len(word) < x:
return False
return True

And this is my solution:

def x_length_words(sentence,x):
new=sentence.split(" ")
for i in new:
if len(i)>=x:
return True
return False

print(x_length_words(“i like apples”, 2))
should print False
print(x_length_words(“he likes apples”, 2))
should print True

My code, returned True and True. But it should return False and True.

Since the length of “i” is less than 2, it will return False, but it returned True.

Any help is appreciated. Thanks.


The for loop in the example show that if one word in that sequence is under it will always be false no matter what.

If you do it your way (with correct indentation when returning false) it will make it true when ever it has one word that meets the requirement, even if there are words not meeting it after that singular word because there is nothing setting back to false.

I tried using your code in this example and had different results to yours. And after tweaking some indentation I found that you might have added an extra indentation on the “return false” line giving it different Boolean expressions that I had mentioned above. But if tweaked to be the right format it would return True and True because there is at least one word that is longer than required.

If you are still confused I can give an example.

Hope this helps!


A link to the exercise would be good to have. Please post it if you can find it again.