[Tutor] Re: building strings of specific length

Andrei project5 at redrival.net
Mon Apr 4 20:57:10 CEST 2005


Vines, John  (Civ, ARL/CISD) wrote on Mon, 4 Apr 2005 10:04:59 -0400:

> Hello. I have a question regarding strings.  How do I format a string to be a specific length?
> For example I need 'string1' to be 72 characters long.

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)

-- 
Yours,

Andrei

=====
Real contact info (decode with rot13):
cebwrpg5 at jnanqbb.ay. Fcnz-serr! Cyrnfr qb abg hfr va choyvp cbfgf. V ernq
gur yvfg, fb gurer'f ab arrq gb PP.



More information about the Tutor mailing list