-1

I have a string, for example

s1 = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse egesta'

s2 = 'Lorem'

and I need to fill it by "_" (or other char) to length which is a multiple of 20, like this:

s1f = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse egesta_____'

s2f = 'Lorem____________________'

How would I do it? I know that I can use ljust but I don't know how to calculate length of the rest.

Zizouz212
  • 4,578
  • 5
  • 36
  • 62
Nips
  • 11,042
  • 18
  • 56
  • 96
  • 1
    Just fill it with `20 - len(sf1) % 20` characters. `len(sf1)` will give you the number of characters in the line. `% 20` will give you the remainder when dividing by 20, and `20 - ` will give you the number of additional characters needed. – sberry Jun 15 '15 at 23:19
  • possible duplicate of [fill out a python string with spaces?](http://stackoverflow.com/questions/5676646/fill-out-a-python-string-with-spaces) – Zizouz212 Jun 15 '15 at 23:36

3 Answers3

4

There is a method for that called ljust:

>>> s2f = 'Lorem'
>>> s2f = s2f.ljust(len(s2f) + (20 - len(s2f) % 20), '_')
>>> s2f
'Lorem_______________'

To reduce the clutter in a single line, you just make a separate function:

>>> def get_multiples(string):
        return len(string) + (20 - len(string) % 20)

Then,

>>> s2f = s2f.ljust(get_multiples(s2f), '_')
Bhargav Rao
  • 41,091
  • 27
  • 112
  • 129
Zizouz212
  • 4,578
  • 5
  • 36
  • 62
  • He want to fill it by "_" (or other char) to length which is a multiple of 20 BUT this just fill the string until reaching 20 as lenght – farhawa Jun 15 '15 at 23:52
  • @farhawa Thanks, missed that part. Edited now. – Zizouz212 Jun 15 '15 at 23:57
  • Dear @Zizouz212 The next time you provide a link to the method kindly mention the name of the method. Do not provide a hyperlink on other words. It would help people discover the documentation link with more ease. I have done it this time for you before upvoting. Regards. – Bhargav Rao Jun 16 '15 at 00:21
  • @BhargavRao Thanks for letting me know. Will do next time and every time afterwards! :D – Zizouz212 Jun 16 '15 at 00:23
0

Here's a function with sberry's "20 - len(sf1) % 20" idea.

def charfiller(the_string, filler_character, multiple_of):
    extra_characters = multiple_of - len(the_string) % multiple_of
    the_string = the_string + (filler_character * extra_characters)
    return the_string


s1 = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse     egesta'
s2 = 'Lorem'
s1_filled = charfiller(s1, '_', 20)
s2_filled = charfiller(s2, '_', 20)
print s1_filled
print s2_filled
print len(s1_filled)
print len(s2_filled)

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse egesta_____ Lorem_______________

80

20

Community
  • 1
  • 1
cameron-f
  • 431
  • 1
  • 3
  • 14
-2

Something like this?

 string_to_modify = "something"
 str_ = ''
 for i in range(20 - len(string_to_modify ) % 20)
     str_ = str_+"_"
 string_to_modify = string_to_modify + str_
farhawa
  • 8,406
  • 16
  • 37
  • 80