I have a list:
lst = ['cat', 'cow', 'dog']
Need to print:
A1 cat
B2 cow
C3 dog
How I can do it?
I have a list:
lst = ['cat', 'cow', 'dog']
Need to print:
A1 cat
B2 cow
C3 dog
How I can do it?
Here is one way.
from string import ascii_uppercase
lst = ['cat', 'cow', 'dog']
for num, (letter, item) in enumerate(zip(ascii_uppercase, lst), 1):
print('{0}{1} {2}'.format(letter, num, item))
# A1 cat
# B2 cow
# C3 dog
Explanation
enumerate
for a numeric counter with optional start value of 1.string.ascii_uppercase
to loop upper case alphabet.zip
and loop through, using str.format
to format your output.One way to accomplish this would be to use ASCII values of the alphabets and increment it over every iteration of the loop.
ascii_char_code = 65
lst = ['cat', 'cow', 'dog']
for index, item in enumerate(lst):
print(chr(ascii_char_code) + str(index) + ' ' + item)
ascii_char_code += 1
The chr() function converts ASCII code to the corresponding character
You can try this simple enumerate
approach :
lst = ['cat', 'cow', 'dog']
chr_s = list(map(chr,range(65,90)))
for i,j in enumerate(lst,1):
print("{}{} {}".format(chr_s[i-1],i,j))
output:
A1 cat
B2 cow
C3 dog