FAQ: Introduction to Strings - Review


#1

This community-built FAQ covers the “Review” 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 Review

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

In this exercise the code that I have written for the first part is this:

def username_generator(first_name, last_name):
first = first_name[:3]
last = last_name[:4]
if len(first_name) > 3 and len(last_name) > 4:
username = first + last
if len(first_name) > 3 and not len(last_name) > 4:
username = first + last_name
if len(last_name) > 4 and not len(first_name) > 3:
username = first_name + last
else:
username = first_name + last_name

Can you pinpoint me the error in this code? Didn’t get the right answer for the first part of the exercise.

For the second part, the right code looks like this:

def password_generator(user_name):
password = “”
for i in range(0, len(user_name)):
password += user_name[i-1]
return password

Wouldn’t this part of the code “password += user_name[i-1]” generate “AbeSimpp” instead of “pAbeSim”? Why?


#3

It’s a bad copy, so no, can’t pinpoint anything in that.

However, I can point out that all of this:

Is completely useless since you already grabbed at most that much

(Codecademy’s solution does something similar, it’s a bit laughable…)


#4

You can implement both those functions with one-liners using only slicing (keep in mind that negative indices counts from the back towards the front, so -1 refers to the last character)
If you didn’t just accidentally leave it out when you pasted the code, then your first one is missing a return statement (so your function has no result at all).


#5

def username_generator(first_name, last_name):
first = first_name[:3]
last = last_name[:4]
if len(first_name) > 3 and len(last_name) > 4:
username = first + last
if len(first_name) > 3 and not len(last_name) > 4:
username = first + last_name
if len(last_name) > 4 and not len(first_name) > 3:
username = first_name + last
else:
username = first_name + last_name
return username

The above function was the code that I used. Probably the iteration might have looked as if it’s hard to pinpoint?
According to the exercise, the first three letters from the first name and the first four letters from the last name should form the username of a person, and if that’s not the case, then their entire name should be returned. How does the code above that I have written wouldn’t fit well with the requirement of the exercise?


#6

I mean it’s different. Can’t say what’s wrong in your code, can’t see it. Can’t reproduce the situation.

No, no. It does. But the requirement is to take up to 3 letters right? You did this:

At that point you’re done. (Plus the first four of the last name)
If you’re done, what does the rest do? Nothing.


#7

Hello Surendazz,

We only want to perform slices on inputs where the first name has at least 3 characters, and the last name has at least 4 characters. If either of the the inputs do not meet these requirements, we will not be performing any slicing. So, we want to check if the length of our inputs meet the slicing requirements:

if len(first_name) < 3 or len(last_name) < 4:
username = first_name + last_name
return username

If the length of first_name OR the length of last_name do not meet the slicing requirements, we can skip slicing and simply concatenate the two inputs. For example, if we were to input (“Ty”, “Jordan”) or (“Adam”, “Lee”) or (“Wu”, “Yan”) the function would detect that one or both of the inputs do not meet the slicing requirements. The function would then return “TyJordan”, “AdamLee”, or “WuYan”.

Now, if first_name and last_name meet the length requirements, we can move on to the slicing operations. For those, we concatenate the first 3 letters of the first name with the first 4 letters of the last name:

else:
username = first_name[:3] + last_name[:4]
return username

In this second half of the function, the input data has already passed the length check, so we know that we can perform the slicing work. We can use first_name[:3] to get the first 3 letters of the first name, and last_name[:4] to get the first 4 letters of the last name.

For instance: “Adam”[:3] would be “Ada” and “Jameson”[:4] would be “Jame”, we then concatenate those into “AdaJame” and return the result.


#8

In the second part, how could I print the result? I try to print (password_generator (username)), but I got a name error

def password_generator (username):
password =
for i in range (0,len(username)):
password += username [i-1]
return password


#9

First, it looks like you setup password as an empty list, not an empty string. Otherwise our functions look the same.

Assuming you wrote a username outside of the function (ex: username = ‘DonTrum’) and called the function (ex: print(password_generator(username)), you should get the result (ex: ‘mDonTru’). Of course this is sort of cheating because it ignores the first function entirely.

I also get a name error if I try to call the first function (which works) and then the second one. To make them work together I don’t write a username outside of the function, and make the first function the input of the second one. i.e print(password_generator(username_generator(first_name, last_name)))

There is probably a better way to go about this, but that is what worked for me.


#10

Replying to the second part of the code:

It’s helpful to think about each loop/iteration separately. For the first loop the computer is at the first indice, the A in in AbeSimp and position 0. The command is to add i - 1 to password. 0 - 1 is -1 and -1 is the last indice, p. So now password = ‘p’. On the second loop the computer is at the second indice, the b in AbeSimp and position 1. The command is, again at i - 1 to password. 1 -1 is 0, and 0 is A. So now password = ‘pA’. The loop carries on from there. ‘pAb’, ‘pAbe’, etc


#11

In second part why we need loop if we can get answer trough slicing.

def password_generator(username):
  password = username[len(username)-1:] + username[:len(username)-1]
  return password

#12

this exercise can be completed without any looping, len(), or range.

for the first function

def username_generator(first_name, last_name):
username = first_name[:3] + last_name[:4]
return username

this works, no need to specify a string length for this task

and for the second function

def password_generator(username):
password = username[-1] + username[:-1]
return password

I wish they had designed the problem so it needed loops and other python capabilities