INTRODUCTION TO STRINGS (Exercise)

Hello, I do not understand the logic behind the solution of the 3rd exercise, if someone can help me i will be really grateful.
link: https://www.codecademy.com/paths/data-science/tracks/dsf-python-fundamentals-for-data-science-part-ii/modules/dsf-python-strings/lessons/introduction-to-strings/exercises/review-i

Thank you !!

Fran <3

Hi,

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

So, in the loop i goes from 0 to the length of ‘user_name’.
For the first loop, i = 0, so user_name[i-1] becomes username[-1] → which is actually pointing to the last letter in user_name. (negative indexes in Python go from back to front, starting at -1).
After that, user_name[i-1] will track through and add the rest of the letters in order.

Hope that’s a little clearer

1 Like

Thank you so much, now I understand it perfectly. You are the best!

1 Like

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.