FAQ: Introduction to Strings - Review

Thanks! I was struggling to understand i - 1, but you explained it perfectly!


def password_generator(username):

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

return password

work for creating a password instead of using a for loop?

Why is the solution given by CodeCademy this:

def username_generator(first_name, last_name):
    if len(first_name) < 3:
        user_name = first_name
        user_name = first_name[0:3]
    if len(last_name) < 4:
        user_name += last_name
        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

Even tho this solution is way easier:

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

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

Hello @ihagok and welcome to the Codecademy Forums!

Your solution works perfectly well. There are multiple ways to go about writing this function and Codecademy’s solution chose to implement a loop.

I tested it, and it doesn’t return any error.

Yes, sorry, my bad. I do think Codecademy was demonstrating a more explicit way of indicating that full names would be used in the case that they were too short. Great code!

Hello everyone, please see my code for the exercise review - introduction to strings. My code was different but solved what was asked for. I wrote my code based on how I interpreted the requirements

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 or len(last_name) <= 4:

  username = first_name + last_name

  return username

def password_generator(username):

password = ‘’

for i in range(0, len(username)):

password = username[-1] + username[:-1]

return password

test = password_generator(‘AbeSimp’)


Hi everyone, I have been a bit stumped these last few exercises and I found a solution I thought was viable and maybe a little cleaner but I have been getting the question wrong. I have tried to pass many different first and last name combinations to debug this but they came out the way they should? Is this whole code wrong or is it wrong purely for this exercise? Thanks in advance everyone! I have attached my code sequence below…

def username_generator(First_name, Last_name):

username = First_name[0:3] + Last_name[0:4]


username_generator(“Abe”, “Simpson”)

username_generator(“AJ”, “Dillon”)

username_generator(“Apollo”, “Ono”)

I don’t know exactly what testing is involved in this lesson but like many others, it asks for these details to be returned (not printed).

Isnt the third task asking to flip the username so if its Abesimp it should be pmisAbe. because in the question it says " To shift the letters right, at each letter the for loop should add the previous letter to the string password … "
I can do this by just placing -i instead of i-1, the format of the question is a bit confusing.

def password_generator(username):

  password = ""

  i = 0

  for index in range(0, len(username)):

    i +=1

    password += username[-i]

  return password


this would reverse the username and put it as the password``

My interpretation of the instructions is that the characters should be shifted as you you mentioned at the start. As you say your solution would reverse the user name but that is not the same as shifting it one to the right.

If I’m not mistaken your output would be "pmiSebA" (if you started from the original name). As per the instructions, the target is "pAbeSim". Characters are shifted one to the right (with the final character bumped round to the start of the string).

thanks for the solutions! I could not understand what question 3 was asking me to do at all.

In this exercise I am also confused by the solution itself. Which portion of the passwordgenerator function removes the last letter?

def username_generator(first_name, last_name):
  username = first_name[:3] + last_name[:4]
  return username
def password_generator(username):
  password = ""
  for i in range(0, len(username)): #Is this selecting only the first 6 letters? How?
      password += username[i-1] #I believe this adds the last letter of username to be the first letter of password
#where did the first six letters join the last letter?
  return password
print(username_generator("stacy", "adams"))

I think the best way to start here would be making sure you know exactly what values your loop gets for i. For this example we have len("stacy") which just gives you the length of the string; in this case, 5.

So what values does range(0, 5) provide? If you’re not certain, check. You’ll likely use range quite often so it’s good to know exactly how it works.

The next thing you need to take note of here is the index used on the username list. It’s i - 1. Negative indices start indexing the list in reverse.

# We know we have three elements in variable "word" below
word = "abc"
# We can access them with, word[0], word[1], word[2]
# A negative index starts from the END of the list
# So word[-1] is the first element counting backwards from the end of the list
word[-1] == "c"
# Since we're stepping backwards
word[-2] == "b"
# So [-3] would be?

Considering the index of [i-1] and the value of i from range what would be the first letter output from username? What about the rest of them?

The “what about the rest of them?” part is lost on me. From what I can see, the only thing adding letters to the password string is:

password += username[i-1]

In my mind this means that the only letter in the string is “m.” I do not see the i value in a range of 0, 1, 2, 3, 4, 5, 6 becoming the remaining letters. I see them being integers. len(username) is just some sequence of numbers. A range of some sequence of numbers are more numbers. i inside of a range of some more numbers is a number across iterations.
I wish I understood. When I think about this, the i-1 is either adding “staada” to the password string or it is adding “m” to the password string, not both. The password string is empty before the for loop. I really do not see the point of the line:

for i in range(0, len(username)):

It seems like this is saying, for each item in the range of 0-7, then add “m” or “staada” - again, not both.

The purpose of integer values is just to select a specific element of a list.

lst = ['red', 'yellow', 'blue']
print(lst[2])  # we use index/subscript 2
Out: blue  # Using an integer we select a specific part of a list

Why not check what value of i-1? Something like print could tell you exactly what index you are addressing with i-1.

When I say:`

password += username[i-1] #this is an index value for m in the password generator input "staadam"

is equivalent to adding an m to the blank string password, am I wrong?
How did the rest of the letters get into the password string if all I did was add m?

The letter added would depend on the value of i-1. Since this line is nestled within a loop the value changes and more than one letter would be added.

I messed around with it some more. If I place a username value of “staadam” in the function call then I get the expected answer of mstaada. I do not really know why. I can set for i in range(0, len(username)) to range(0, 7) and get the same answer. Check.
When I return i, instead of password, then I get ‘none’. I would expect this to be the rest of the values in the string since i appears to be the only other input to the password string using the += argument. I am still wrong, however, because the value is ‘none’. Where the “staada” part of the string goes and how it returns to the password string is still a mystery to me.
i is getting added 7 times, whatever it is. If I leave the value as username[-1] then I return mmmmmmm. Somehow i is staying intact as the rest of the username input. The order I would expect is a complete reverse dissection if it is a loop.
Whatever is going on, I will just remember that i is going to be some static value of the string input for the function and += of some index value for that string is going to take that portion and place it at the front, leaving the rest intact.
This is completely defeating me. I am already 3 sections past this and cannot figure out this apparently minor detail.

Thanks for explaining it. Was struggling to understand how it works on the i-1