FAQ: Introduction to Strings - Review

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!

10 posts were split to a new topic: When should I use slicing?

2 posts were split to a new topic: Why can’t I print?

16 posts were split to a new topic: Is it necessary to use a loop?

2 posts were split to a new topic: What’s wrong with this code?

My solution, hope this helps

def username_generator(first_name, last_name):
if(len(first_name) < 3 or len(last_name) < 4):
return first_name + last_name
else:
return first_name[:3] + last_name[:4]

username = username_generator

def password_generator(username):
#Second solution
password = username[1:] + username[0]

####################################

#third solution with for loop
password = “”
for i in range(len(username)):
password = password + username[i-1]
return password

1 Like

4 posts were split to a new topic: Are strings actually immutable?

Chipping away:

def username_generator(fname, lname):
  return fname[:3] + lname[:4]

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

Why is this code not working?

def username_generator(first_name, last_name):
  for index in range(len(first_name)):
    for index2 in range(len(last_name)):
      if index > 2 and index2 > 3:
        username = first_name[:3] + last_name[:4]
        return username
      elif index <= 2:
        username = first_name + last_name[:4]
        return username

im getting

“Expected the name Mimiko Watanabe to produce the username MimWata, instead got MimikoWat”

Raw code is preferrable to an image. Are we to type your code in to test it? Please post the code in a reply.

Ok, I just edited it. Will keep in mind

1 Like

Not sure you need a nested list. There should be only two steps to arrive at a username.

first

will be first_name if its length is less than 4, else it will be the first three letters of the name.

last

will be last_name if its length is less than 5, else it will be the first four letters of that name.

1 Like

Yeah I guess me using a loop was the problem.

I redid it with this code and I finally solved it:

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

And if the last name is short, with a long first name?

1 Like

Yeah I was going to add that but it accepted this. I’m not even sure why my first one didn’t even work or why this worked but it did.

I’m confused about when to use range() and when to use len() in these exercises. Can someone explain why this is wrong, for the first step in the exercise? I’m reasonably sure it has something to do with the steps where I use “range” but I don’t know why or how to fix it.

Also, can anyone tell me how to indent, in these replies/questions? Not being able to indent is making me crazy. :slight_smile:

def username_generator(first_name,last_name):
if len(first_name) > 3:
part1 = first_name[:3]
else:
part1 = range(len(first_name))
if len(last_name) > 4:
part2 = last_name[:4]
else:
part2 = range(len(first_name))
username = part1 + part2
return username