0

I need to put a space between the list (a cat's name) and the index.

Right now it comes out like this:

Pussy0
Pinky1
Fats2

I want it to print out like this:

Pussy 0
Pinky 1
Fats 2
catNames = []
while True:
    print('Enter the name of cat ' + str(len(catNames) + 1) +
      ' (Or enter nothing to stop.):')
    name = input()
    if name == '':
        break
    catNames = catNames + [name]  # list concatenation
print('The cat names are:')
#for name in range(len(catNames)):
    #print(name)

for i in range(len(catNames)):
    print(catNames[i] + str(i))

4 Answers4

2

Just do this:

catNames = []
while True:
    print('Enter the name of cat ' + str(len(catNames) + 1) +
      ' (Or enter nothing to stop.):')
    name = input()
    if name == '':
        break
    catNames = catNames + [name]  # list concatenation
print('The cat names are:')
#for name in range(len(catNames)):
    #print(name)

for i in range(len(catNames)):
    print(catNames[i] + " " + str(i))

Though its always better to use f-strings or format:

catNames = []
while True:
    print('Enter the name of cat ' + str(len(catNames) + 1) +
      ' (Or enter nothing to stop.):')
    name = input()
    if name == '':
        break
    catNames = catNames + [name]  # list concatenation
print('The cat names are:')
#for name in range(len(catNames)):
    #print(name)

for i in range(len(catNames)):
    print(f"{catNames[i]} {str(i)}")

Using f-strings makes the code much cleaner, and simpler to understand. Look here for more details. Note that f-strings are only for python 3.6 or above. If you have a version of python below 3.6, check my previous answer to see how you can use f-strings in below python 3.6.

xilpex
  • 3,097
  • 2
  • 14
  • 45
  • The answer where perfect I want to know more about formating, Im looking forward to clicking on the link you gave me. Thank you Xipex – Peter Cockram May 16 '20 at 19:41
  • I used this as the answer - print(str(i),":",catNames[I]) it worked out for me - But thanks for the advanced help. I need to learn a bit about formating - But Good work! and Thanks – Peter Cockram May 16 '20 at 19:44
2

Try string formatting:

catnames = ['Fuzzy', 'Pinky', 'Fats']

for i, cname in enumerate(catnames):
    print('{} {}'.format(cname, str(i)))
S3DEV
  • 8,768
  • 3
  • 31
  • 42
  • Im sorry but that just broke the code making it come up with this error message: line 14 print(‘{} {}’.format(catNames[i], str(i))) ^ SyntaxError: invalid character in identifier Thank you anyway. – Peter Cockram May 16 '20 at 19:34
  • @PeterCockram - Sorry mate. Somehow the single quote characters got replaced with something else. (Answered from my iPhone, that might be why). Fixed. – S3DEV May 16 '20 at 19:43
1

I would suggest using string interpolation for this:

for i in range(len(catNames)):
    print(f"{catNames[i]} {i}")
Simon Martineau
  • 210
  • 2
  • 8
0

I with the help of "Demolution Brother" showed me how to write a list in a string followed by an index.

print(str(i),":",catNames[I]) 
  1. print function
  2. Turn the interfere into a string - (str(
  3. After the str( we now have the interfere (str(I)
  4. Important to use the , to separate the string to put in " : ". It has to be done with double-quotes.
  5. Now after the comma , we can continue with catNames[I]) (The List)
  6. List item now prints after the index.
  7. Answer - print(catNames[i], " : ", str(I))

Some of the other ways to it was tried out:

    #print(str(i),":",catNames[i])
    #print(catNames[i], i, end=' ')
    #print(catNames[i],end=' ' + str(i))

The code -cat_names

catNames = []
while True:
    print('Enter the name of cat ' + str(len(catNames) + 1) +
      ' (Or enter nothing to stop.):')
    name = input()
    if name == '':
        break
    catNames = catNames + [name]  # list concatenation
print('The cat names are:')
#for name in range(len(catNames)):
    #print(name)

for i in range(len(catNames)):
    print(str(i),":",catNames[i])
    #print(catNames[i], " : ", str(i))