how would you generate an 8 character password with 4 random alphabet and 4 random number using slicing and concatenation. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

how would you generate an 8 character password with 4 random alphabet and 4 random number using slicing and concatenation.

alphabet =“ABCDEFGHIJKLMNOPQRSTUVQXYZabcdefghijklmnopqrstuvwxyz” random_number = (1, 999)

25th Mar 2019, 12:34 PM
banna101
17 Answers
+ 1
Which random functions are you allowed to use? I don’t see any way of choosing random letters just from slicing your alphabet string.
25th Mar 2019, 5:41 PM
Russ
Russ - avatar
+ 1
I think I’ve figured it out using first_letter = alphabet[random.randint(0,len(alphabet)-1)] To get 4 i copied and paste and change the name to second_letter, third and fourth. It seems to work?
25th Mar 2019, 7:25 PM
banna101
+ 1
Thanks to you 🙏🙏
25th Mar 2019, 7:36 PM
banna101
0
Any attempt of your own? You’re far more likely to get assistance if you show us that you’ve made an attempt to solve it yourself.
25th Mar 2019, 2:54 PM
Russ
Russ - avatar
0
import random alphabet = “ABCDEFGHIJKLMNOPQRSTUVQXYZabcdefghijklmnopqrstuvwxyz” rand_index = random.randrange (0, len(alphabet)) first_slice = alphabet[:rand_index] alphabet = first_slice random_number = random.randrange(1, 999) print(“Your password is: “, alphabet + str(random_number)) output should be 4charcters of alphabet and 4 characters of random number with extend of zero So for example tziy0098 or gtjs0999 However the real result is abc715 I can’t figure out how to get only four random alphabet And how to zero extend the number so i generate four characters of random number. Please help
25th Mar 2019, 5:10 PM
banna101
0
For the letters: alphabet = list(first_slice) random.shuffle(alphabet) alphabet = “”.join(alphabet[:4]) For numbers: random_number = (“0000” + str(random_number))[-4:] Does this satisfy your criteria?
25th Mar 2019, 5:34 PM
Russ
Russ - avatar
0
Thank you and Instead for alphabet we can only use slicing method. Like removing random characters but i wanna have 4 random alphabet everytime how would i do that??
25th Mar 2019, 5:37 PM
banna101
0
Thank youuuuu so muchh
25th Mar 2019, 5:37 PM
banna101
0
I can generate 4 seperate letters from alphabets and concatenate them. But i don’t how i would do that
25th Mar 2019, 5:47 PM
banna101
0
Can you set up a for loop? string = “” for i in range(4): string += alphabet[random.randint(0,len(alphabet)-1)] alphabet = string
25th Mar 2019, 5:50 PM
Russ
Russ - avatar
0
We aren’t allowed to use loop yet. So far i can only generate 4 random alphabets By generating rand_index and slicing them
25th Mar 2019, 5:56 PM
banna101
0
I meant random.randrange
25th Mar 2019, 5:58 PM
banna101
0
If used random.randrange i keep getting more than 4 alphabets or less than 4 alphabets. So maybe it’s not random.range Maybe slicing them but how would i slice to get one random character at a time and concatenate them
25th Mar 2019, 6:00 PM
banna101
0
Unless I’m missing something, there doesn’t seem to be a way of doing this by slicing without shuffling the order of your alphabet. If you can’t use a loop, can’t you just use the line I put inside the loop 4 times? I still feel like I’m in the dark about what random functions can/can’t be used.
25th Mar 2019, 7:05 PM
Russ
Russ - avatar
0
alphabet[random.randint(0,len(alphabet)-1)] this might work?
25th Mar 2019, 7:07 PM
banna101
0
Hello i have an example name = “apple pie” first_part = name[0:4] —->instead how would you put random instead of giving the actual location? print(first_part)
25th Mar 2019, 7:17 PM
banna101
0
If you’ve got what you need, great! Congrats!
25th Mar 2019, 7:34 PM
Russ
Russ - avatar