0

I have a list with some book names and their authors, but I want it to look organized, so I want it to take the book title, then complete it with spaces so it ends up with 100 characters, independent of the length of the book title. It would, then, add the book title.
So far I've tried this:

for i in range(0, len(bookList)):
    t = 100 - len(bookList[i])
    numbofspaces = ""
    for j in range(0, t):
        numbofspaces += " "

    s.append(bookList[i] + numbofspaces + authorList[i])

When I tried it in the python shell it worked pretty well, but when it takes the titles from the list, it doesn't work, why is that?

4
  • maybe the list is empty? Commented May 7, 2013 at 18:10
  • no, it's not. I mean, it prints the book title, and the author name, but the number of spaces in between doesn't make it complete 100 characters Commented May 7, 2013 at 18:16
  • Are there unicode characters? I'm not sure how python handles the length of them, this could possibly cause it. Commented May 7, 2013 at 18:19
  • There are line breakers ('\n') and appearently they are the cause of the problem. Commented May 7, 2013 at 19:51

4 Answers 4

7

use string method : str.rjust(100)

>>> x = [ 'charles dickens','shakespeare','j k rowling']
>>> for name in x:
...     print(name.rjust(50))
... 
                                   charles dickens
                                       shakespeare
                                       j k rowling
Sign up to request clarification or add additional context in comments.

2 Comments

I think ljust may be more appropriate if the spaces should be on the right of the variable-length part. Also, neither of the justification methods will fix a name that is too long, so if there's a risk of a title longer than 100 characters in the list of books, you'll need to trim it before justifying.
The thing is, it works when I try it on the shell, printing the string, but when I actually use it on my code it just doesn't do it right.
7

While str.ljust()/str.rjust() are good, simple solutions if this is all you want to do, it's worth noting that if you are doing other formatting, you can do this as a part of string formatting:

>>> '{:<30}'.format('left aligned')
'left aligned                  '
>>> '{:>30}'.format('right aligned')
'                 right aligned'

From the docs.

1 Comment

for the specific question, this method seems like the best fit.
0

while this might not really be the answer to your question, you could just use

booklist[i].ljust(100)

and be done with it.

Comments

0

Besides from the unexplained failure, you have some really complicated code where you don't need it:

range(0, i) can be written as range(i)

You can write the for loop better:

for i, book in enumerate(bookList):
    t = 100 - len(book)
    numbofspaces = ""
    for j in range(t):
        numbofspaces += " "

    s.append(book + spaces + authorList[i])

You can multiply a string: 'a' * 10 == 'aaaaaaaaaa'

And, like @DhruvPathak said, you can just use ''.r/ljust().

1 Comment

Or use zip to iterate over the book and author lists together.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.