+ 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
8 RĂ©ponses
+ 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)
+ 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)
+ 8
# ;)
print(alphabet + ('000' + str(random_number))[-4:])
+ 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
+ 1
Oh i see thank youu
0
The thing is we cannot use .zfill() yet
We can only use concatenation
0
Thank you so muchh
0
Thank youuu so muchhhhh