简体   繁体   中英

In Python how do I make a space between a list and string?

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))

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-string s 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-string s 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.

Try string formatting:

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

for i, cname in enumerate(catnames):
    print('{} {}'.format(cname, str(i)))

I would suggest using string interpolation for this:

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

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))

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