List of 2 letters 2 numbers?

Question:

I’m looking for a list of 2 letters and then 2 numbers. Something like:

aa00
aa01
aa02
aa03
aa04
ect.

I am using this for a Python program that picks a random 4 digit number, a random 4 letter string, and a random 2 letter 2 number string.

I tried combining half of the first two, but it said that it could not add 'int' and 'str' objects. I couldn’t convert the numbers to a string, because they are used for a mathematical equation later in the program.

Asked By: cjsn6

||

Answers:

You can turn the number into a string just for the concatenation:

result = letters + str(number)

But if you want to have the numbers 0 through to 9 work too you probably want to zero-pad the number. You could use string formatting here, using str.format():

result = '{}{:02d}'.format(letters, number)

Both approaches leave the number variable untouched; it is still referencing a number.

Answered By: Martijn Pieters

May be this could help you

Code 1 :

import random as rn

num = ''
stg = ''

for i in range(2):
    num = num + str(rn.randrange(0,9))

for i in range(2):
    stg = stg + ''.join(rn.choices('abcdefghijklmnopqrstuvwxyz'))

result = num+stg

print(result)

Code 2 : You can do it in one for loop as well

import random as rn

num = ''
stg = ''

for i in range(2):
    num = num + str(rn.randrange(0,9))
    stg = stg + ''.join(rn.choices('abcdefghijklmnopqrstuvwxyz'))

result = num+stg

print(result)

Incase any issue in code try to edit it

Answered By: codeholic24
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.