简体   繁体   中英

List of 2 letters 2 numbers?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM