+ 1

how to generate 4 random alphabets and 4 random mumbers

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:12 PM
banna101
8 Respostas
+ 10
Although it's a bit weird way of doing this, just add .zfill(4) to your print statement to include a trailing zero. so print ..... + str(random_number).zfill(4)
25th Mar 2019, 5:27 PM
Kuba SiekierzyƄski
Kuba SiekierzyƄski - avatar
+ 8
Use random.sample to get four random letters of the alphabet. If you can't use zfill, check the length of str(random_number) and add (4-len)*'0' (on the left side)
25th Mar 2019, 5:37 PM
Anna
Anna - avatar
+ 8
# ;) print(alphabet + ('000' + str(random_number))[-4:])
25th Mar 2019, 6:33 PM
Kuba SiekierzyƄski
Kuba SiekierzyƄski - avatar
+ 1
or you can simply use a f str, that replaces % and str.format: [print(f'-->{k:0>4d}<--') for k in [6,66,666,6666,66666]] using str.format would be similar: [print('-->{0:0>4d}<--'.format(k)) for k in [6,66,666,6666,66666]] in your case print(alphabet+f'{random_number:0>4d}') check out for more details: https://docs.python.org/3/library/string.html#formatstrings
25th Mar 2019, 9:09 PM
Edward
Edward - avatar
+ 1
Oh i see thank youu
25th Mar 2019, 9:11 PM
banna101
0
The thing is we cannot use .zfill() yet We can only use concatenation
25th Mar 2019, 5:29 PM
banna101
0
Thank you so muchh
25th Mar 2019, 5:40 PM
banna101
0
Thank youuu so muchhhhh
25th Mar 2019, 6:40 PM
banna101