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?"



_______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor

Reply via email to