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
Learn Python 3

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

10 Likes

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
4 Likes

7 posts were split to a new topic: Can you help with my code?

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

2 Likes

The way this exercise wants you to complete it is quite complicated. I was able to write both functions using only 6 lines of code. In my code I’ve used f-strings (which haven’t been introduced yet), but it could be accomplished just as easily with string concatenation.

def username_generator(first_name, last_name):
  username = (f"{first_name[:3]}{last_name[:4]}")
  return username

def password_generator(username):
  password = (f"{username[-1]}{username[0:len(username) - 1]}")
  return password
3 Likes

A post was split to a new topic: Why doesn’t this code resolve the temporary password lesson?

The exercise says to implement password_generator using a loop, but slicing is much cleaner!

def password_generator(username):
  return username[-1] + username[:-1]
17 Likes

Why do the first pair of print statements work and second not work?

def username_generator(first_name, last_name):
    if len(first_name) < 3:
        user_name = first_name
    else:
        user_name = first_name[0:3]
    if len(last_name) < 4:
        user_name += last_name
    else:
        user_name += last_name[0:4]
    return user_name
  
    
def password_generator(user_name):
    password = ""
    for i in range(0, len(user_name)):
        password += user_name[i-1]
    return password

print(username_generator("Joe","Smith"))
print(password_generator(username_generator("Joe","Smith")))

username_generator("Joe","Smith")
print(user_name)
password_generator(username_generator("Joe","Smith"))
print(password)

I get the following error for the second pair of print statements:
NameError: name ‘user_name’ is not defined
(and the same error for ‘password’)

user_name can only be accessed inside the function username_generator which is local scope unless you declared a variable user_name in the global scope which is outside of the functions!

1 Like

If we do not want to use a loop is this solution viable?


def password_generator(username):

  return username[-1]+username[0:-1]
2 Likes

Hi! I found your solutions very helpful! However, I didn’t quite understand how the password_generator() works and output the letters shifted to the right. Could you please explain a bit? Thanks!

4 Likes

Since the loop is “i in range(length of username)”, the very first i would be 0. The goal is to use the last letter as the first letter, then the rest of the string in order.

So, the first iteration is i - 1, or 0 - 1, which is -1. The value -1 points to the last value of the index of a string or array. If I had the name “Steve” it would target the last “e” first. Then, we add 1 to i as the next part of the loop, making the new value 1 - 1, or 0, which is the S. This will loop until it finishes with eStev. It stops after 5 loops because len(“Steve”) = 5.

15 Likes

Thanks for the explanation! Much clear now. So I can deduce that password = "" will only contain 5 letters because len(“Steve”) = 5 and that’s when the loop stops, right? giving the "eStev" output.

2 Likes


“Go Enjoy”

1 Like