1

I have been working on a program which asks the user to type in a list of names. After they have typed in those names, my program has to right align all of those names. This is what I have so far:

names =[]

# User is prompted to enter a list of names
name = input ("Enter strings (end with DONE):\n")
while name != 'DONE':
    names.append(name) # This appends/adds the name(s) the user types in, to names
    name = input("")

print("\n""Right-aligned list:")
for name in names:
    maximum = max(names, key=len) #This line of code searches for the name which is the longest
    new_maximum = len(maximum) #Here it determines the length of the longest name
    diff = new_maximum - len(name) #This line of code is used to subtract the length of the longest name from the length of another different name
    title = diff*' ' #This code determines the open space (as the title) that has to be placed in front of the specific name
    print(title,name) 

Here is the program without all of the comments:

names =[]

name = input ("Enter strings (end with DONE):\n")
while name != 'DONE':
    names.append(name)
    name = input("")

print("\n""Right-aligned list:")
for name in names:
    maximum = max(names, key=len) 
    new_maximum = len(maximum) 
    diff = new_maximum - len(name)
    title = diff*' '
    print(title,name) 

The output I want for this program is:

Enter strings (end with DONE):
Michael
James
Thabang
Kelly
Sam
Christopher
DONE

Right-aligned list:
    Michael
      James
    Thabang
      Kelly
        Sam
Christopher

Instead this is what I get:

Enter strings (end with DONE):
Michael
James
Thabang
Kelly
Sam
Christopher
DONE

Right-aligned list:
     Michael
       James
     Thabang
       Kelly
         Sam
 Christopher

NB:The moment the user types in DONE, the prompt ends.

The problem is that there is an extra space printed for every name in the list. How can I print it right-aligned but without the extra spaces?

Jeff Mercado
  • 129,526
  • 32
  • 251
  • 272
user3556825
  • 15
  • 1
  • 3
  • 1
    What's the problem? You pretty much have the same output... – Jeff Mercado Apr 21 '14 at 14:11
  • One too many spaces to left of each word, but I have the correct answer now. Thanks anyway. – user3556825 Apr 21 '14 at 14:33
  • You have a good answer here for this problem. However, to simply correct the problem in your version, it was just a matter of having that extra space being a problem. That extra space came from using multiple arguments to the `print()` function. All arguments will be separated by a space. What you would have wanted to do there was to concatenate the `title` and `name` variables and print that. – Jeff Mercado Apr 21 '14 at 15:16

2 Answers2

1

You can use string formatting as follows:

a = ['a', 'b', 'cd', 'efg']

max_length = max(len(i) for i in a)

for item in a:
    print '{0:>{1}}'.format(item, max_length)

[OUTPUT]
  a
  b
 cd
efg
sshashank124
  • 31,495
  • 9
  • 67
  • 76
1

I know this is an old question, but this will get it done in one line:

print('\n'.join( [ name.rjust(len(max(names, key=len))) for name in names ] ))

This SO answer for list comprehension helped me: Call int() function on every list element?

rreagan3
  • 117
  • 6