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?