FAQ: Introduction to Strings - Review

Because len(“Steve”) = 5 is used with range you end up with range(len(“Steve”)). So range with no start will start with 0 and go to 5 because of the length of “Steve”. It’s about the same as using range(0, 5).

Hey,

I’m struggling to understand when we should use range to iterate through indices rather than the normal method.

I did this exercise like that:

> def password_generator(user_name):
> 
>     pass = ''
> 
>     for char in user_name:
> 
>         pass += user_name[char-1]
> 
>     return pass

And the correct way was this:

> def password_generator(user_name):
> 
>     pass = ''
> 
>     for char in range(0, len(user_name)):
> 
>         pass += user_name[char-1]
> 
>     return pass

I thought that

> "for i in string"

would iterate throughout the length of that string? When do we need to use “range” and “len” instead of the normal method?

Sorry for the beginner question.

They are both very different. The first example would not work because its going to take the user_name that is passed to the function password_generator say “Lucas”. The for loop is going to iterate through the string “Lucas” starting with the first pass and put L in the temporary variable char.
At this point its fine but when it goes to use the temporary variable char in the next statement you can’t do L - 1 because L is a string and 1 is an integer. This is going to give you a TypeError.

In the second example the for loop is using all integers and will not have that issue. First you find the length of your user_name string that is passed to the function and using that number with range you can create a range of numbers to use each time though the for loop.
So if your user_name string that was passed into the function had a length of 6 you would use range and the first time char would be 0 than 1 and 2 and so on until you get to the length of your user_name string. Doing this and using the number in the next statement you can now run char - 1 because you will not get an error running 0 - 1 than 1 - 1 and so on. This number will be used for your user_name index location and will store the information from that index in pass each time.

You can test this from adding print(char) following the for loop to watch what happens to char each time through the loop.

The last thing you want to watch out for is using the word pass as your variable name. Python has something called a pass statement so the key word is already defined in python and can’t be used.

3 Likes

Is there a way to have the password in reverse order ? like if the username is “steve”, generated password to be “evets”

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

google.com/search?q=python+reverse+string

1 Like

and as for your code you might consider what indices would be required and compare to which indices you are using

Got it. Thanks

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


password_generator("Steve")

uhm. if the 6th line is reversing the password then what is the rest doing?

If you use slicing, then there would be nothing else to do, so that would be the only line in your function.

Is your function supposed to print something or have a result? It should probably only be one or the other.

If you were to implement reversing yourself, then you’d want to place the characters in a list so that you can move things around (strings are immutable) and then when you’re done you’d create a string from that list of characters

Thanks for the idea about List.
Find few good ways of reversing the string here using slicing, loop and join method.

String reversing

This is super weird:

def password_generator(username):

password = “”

for x in username:

index = username.index(x)

password += username[index - 1]

return password

print(password_generator(“AbeSimp”))

print(password_generator(“ZoeLee”))

gives me:

pAbeSim
eZoeoo

How can it be giving me the right answer for the first example but not the second? I’m sure I’m doing something dumb but what?

Why not print from your loop as it executes? It will give you a better idea of what’s going on. Have a little look at exactly how the .index method operates. How could this affect the result with different strings?

2 Likes

Of course - pulling the index for where a character appears in a string doesn’t work if the character appears more than once, because it’ll give you the first time the character appears instead. Obvious once it’s pointed out, but it was driving me mad - thanks!

2 Likes

Hi,
I have tried solving program in this below given way. I am getting desired output, but not sure if I am going in the right direction.

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


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


user = username_generator("ABCKJIU","3948473")
print(user)
print(password_generator(user))

Looking forward for any type of inputs.
Thanks in advance.

1 Like

Hello there :wave:!

For the username_generator function, your program would work fine for any input where the length of the user’s first name is three or more and if their last name is four or more characters long. However, your function won’t fulfill the following requirement: “If their first name is less than three letters or their last name is less than four letters it should use their entire names.”

For example, if the user’s name was Al Smith, your function would return a username of “AlSmith” because the len(first_name) < 3 statement is satisfied. In this case, the full last name was printed instead of just the first four letters. How might you fix this problem?

As for your password_generator function, it will produce the correct output but there is some code you could cut out since it is redundant. Can you figure out what to remove?

1 Like

Hi,
Thank you so much for replying. I have understood, where is have done mistake. This time I have tried different combinations to get the output. Below is my code.

def username_generator(first_name, last_name):
    
    if len(first_name) < 3 and len(last_name) < 4:
        username  = first_name + last_name
     
    elif len(first_name) < 3 and not len(last_name) < 4 :
        username = first_name + last_name[:4]
   
    elif len(first_name) > 3 and len(last_name) < 4:
        username = first_name[:3] + last_name
        
    else:
        username = first_name[:3] + last_name[:4]
    return username
        
print(username_generator("ABCKJIU","defghjk"))
print(username_generator("ab","rik"))
print(username_generator("we", "ritutfjf"))
print(username_generator("DFGKRERO", "45"))

When I have compared my program with codecademy program, I observed that even without using ‘+ operator’ username with both first_name and last_name can be returned.

As for password_generator function, the code ‘username[-1] + username[:-1]’ can be written as ‘username[i-1]’. Below is my modified code -

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

user = username_generator("ABCKJIU","3948473")
print(user)
print(password_generator(user))

Thanks.

Yep, great job on fixing your code :clap:! Your username_generator accounts for most possibilities password_generator function doesn’t contain anymore redundant code. However, your username_generator is just missing a few characters. If you look at your fixed code, what would happen if someone’s first name was exactly 3 characters long or if their last name was exactly 4 characters long?

1 Like

Hi,
Yes, I have fixed that in my below program by adding ’ = ’ sign.


def username_generator(first_name, last_name):
    
    if len(first_name) <= 3 and len(last_name) <= 4:
        username  = first_name + last_name
     
    elif len(first_name) < 3 and not len(last_name) < 4 :
        username = first_name + last_name[:4]
   
    elif len(first_name) > 3 and len(last_name) < 4:
        username = first_name[:3] + last_name        
        
    else:
        username = first_name[:3] + last_name[:4]
    return username
        
# print(username_generator("ABCKJIU","defghjk"))
# print(username_generator("ab","rik"))
# print(username_generator("we", "ritutfjf"))
# print(username_generator("DFGKRERO", "45"))
# print(username_generator("ABC", "3459"))

def password_generator(username):
    password = ""
    for i in (range(len(username))):
        password = username[-1] + username[:-1]
    return password
     
user = username_generator("ABC","3948")
print(user)
print(password_generator(user))

Thanks again. :slightly_smiling_face:

Yes. I think that you’re still missing a few more “=” signs.

elif len(first_name) <= 3 and not len(last_name) <= 4:

&

elif len(first_name) > 3 and len(last_name) <= 4:
1 Like

ohh… Yess. I should have done that too. I really need to pay attention to each line of code.

1 Like