0

I have a list:

lst = ['cat', 'cow', 'dog']

Need to print:

A1 cat
B2 cow
C3 dog

How I can do it?

Anon Anon
  • 13
  • 1
  • 2
    Read about loops and how to iterate over lists in Python. What did you try? What problems did you run into? – Omer Tuchfeld Apr 01 '18 at 18:24
  • 2
    `print("A1 cat\nB2 cow\nC3 dog")` solves this particular case, but is probably not very well generalizable. What are the `A1`,...,`C3` strings, how do they depend on the names of the animals? – Andrey Tyukin Apr 01 '18 at 18:25

3 Answers3

0

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

  • Use enumerate for a numeric counter with optional start value of 1.
  • Use string.ascii_uppercase to loop upper case alphabet.
  • zip and loop through, using str.format to format your output.
jpp
  • 159,742
  • 34
  • 281
  • 339
0

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

Daksh
  • 1,064
  • 11
  • 22
0

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
Aaditya Ura
  • 12,007
  • 7
  • 50
  • 88