[Tutor] building strings of specific length

Max Noel maxnoel_fr at yahoo.fr
Mon Apr 4 21:13:57 CEST 2005


On Apr 4, 2005, at 16:04, Vines, John (Civ, ARL/CISD) wrote:

> 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.
>
> Thanks for your time,
> John

	You can use the string methods ljust, rjust and zfill:

 >>> "xxx".rjust(10)
'       xxx'
 >>> "xxx".ljust(10)
'xxx       '
 >>> "xxx".zfill(10)
'0000000xxx'

	Note that the string will not be truncated if it's more than (here) 10 
characters long, but you can use slicing to get around that:

 >>> "xxxxxxxxxxxxxxxx".rjust(10)[:10]
'xxxxxxxxxx'


-- Max
maxnoel_fr at yahoo dot fr -- ICQ #85274019
"Look at you hacker... A pathetic creature of meat and bone, panting 
and sweating as you run through my corridors... How can you challenge a 
perfect, immortal machine?"




More information about the Tutor mailing list