FAQ: Introduction to Strings - More and More String Slicing (How Long is that String?)

This community-built FAQ covers the “More and More String Slicing (How Long is that String?)” 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 More and More String Slicing (How Long is that String?)

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: Could this be solved just using [-3:]?

2 posts were split to a new topic: How does slicing using subtraction from the length work?

3 posts were split to a new topic: Is this not an error?

why do they use len when getting the index? Surely you do not need to know the length of a string ? you just need the last 3 [-3:] ??

def password_generator(first_name, last_name):
  temp_password = first_name[len(first_name)-3:] + last_name[len(last_name)-3:]
  return temp_password

This is my code -

def password_generator(first_name, last_name):
  first_name_last_3 = first_name[-3:]
  last_name_last_3 = last_name[-3:]
  return first_name_last_3 + last_name_last_3

PS i know i can shorten this code by not using temp variables, This is something im working on :smiley:

3 Likes

I was thinking the exact same thing.
I’m not sure what the purpose of using length while slicing.
I guess just to know multiple ways to skin a cat??
No idea.

first_name = “Reiko”

last_name = “Matsuki”

def password_generator(first_name, last_name):

return first_name[-3:] + last_name[-3:]

temp_password = password_generator(first_name, last_name)

print(temp_password)

my code is this but i just dont understand why i need length tho

2 Likes

Not sure what you mean by length. Can you elaborate, please?

the instructions want you to learn and use the len() function to get the number of characters in the string and then subtract a number from that to give it the index of where to start the slice.

it is a little silly in the use of this exercise because you can just use string_name[-3:] to get the last 3 characters of the string (as many have already mentioned)

but suppose you need to cut the string in half no matter how many characters you’re give, in that case you would want to know how to use len() and then divide by 2, etc. really this exercise is just like anything else in school, the more you use something, even if its overkill, the more you’ll actually learn it and remember it.

3 Likes

I had this question in functions earlier and I thought it would eventually snap, but it hasn’t. I’m missing these exercises on the same issue, assigning the function to a variable outside of the function. The password_generator creates the temp_password variable. Why must the temp_password be defined again with the call of the function instead of calling the function?
This produces the expected result:
temp_password = password_generator(first_name, last_name)
print(temp_password)

This prints the location of the function but this is what I expected to work:
password_generator(first_name, last_name)
print(password_generator)

The first example gives the return value of the function somewhere to go. The second example calls the function but does not assign the return value. In the last print statement, password_generator is not the name of a variable, but a reference to the function.

assign_return_here = call_function(arguments)

Thank you! I appreciate your response

Yep, same thing here.
I imagine they want us to know that we can have an index to work with. Say for example you needed to extract the middle part of the list. But then an exercise where we have to do that would show a better example on why that is necessary.

Nah, I rather ask why EVERY time I feel like it, that’s part of learning and should never be discouraged. I’m actually surprised you are doing it being a moderator.

BTW, the ‘why’ in this case is explained in the very next lesson, is is to show you that there is a hard way and an easy way. For anyone else wondering the same question.

2 Likes

Reinforcing the need to follow lessons, complete the exercises and move on. The why questions can come up during a unit review, if any still exist. If we populate all the narrative with reasons why it will pollute the rudimentary learning process with information overload. Rote first, muscle memory and rudimentary understanding of syntax and basic usage is far more important than the reasoning behind it, at that stage. Most times the why of things reveals itself in due course of time once the learner has had some practice and developed a broader view of the landscape.

2 Likes

If you want to do it all in a single line, you could do it like this:

print(password_generator(first_name, last_name))

I did this, instead of using len(), and it did not give an error. But, I had to look at the solution, in order to move on from the lesson.

full_name = first_name[-3:] + last_name[-3:]

This exercise is broken. I write a code that returns the correct answer and the exercise says it returns nothing. My code works in VS code. I can’t pass this exercise due to the fact that even hitting “replace” on what is the “correct” (aka your code is exactly like ours) answer. If I could go back I would NOT pay for pro.

Out of curiosity, can you share your code? At the bottom of the exercise, there should be a “Copy to Clipboard” button.

Posting directly into the forums will mess up the formatting and indentation.To paste the code in the forums with proper formatting, see: How do I format code in my posts?

Alternatively, you can upload a screenshot.

Also, what does the feedback/error message that pops up at the bottom say?

1 Like

I got it to work by refreshing the browser without cache and repasting my code