[Tutor] Re: building strings of specific length
jfouhy at paradise.net.nz
jfouhy at paradise.net.nz
Tue Apr 5 00:22:40 CEST 2005
Quoting Andrei <project5 at redrival.net>:
> You could do (I'll use 20 instead of 72 to keep it readable):
>
> >>> s = "%20s" % "xyz"
> >>> len(s), s
> (20, ' xyz')
>
> or:
>
> >>> s = "xyz"
> >>> s = s + (20 - len(s)) * ' '
> >>> s, len(s)
> ('xyz ', 20)
Just a comment ---
You can do the second example using string formatting as well:
>>> s = '%-20s' % 'xyz'
>>> s, len(s)
('xyz ', 20)
--
John.
More information about the Tutor
mailing list