[FRPythoneers] dynamically created format strings?

Matthew D. Wood woodm at tiora.net
Mon Jan 26 11:27:50 MST 2004


I'm just curious, but does it need to be a format string?  If you are at all
comfortable converting your numbers to strings, there are 4 functions you
may want to look at:

[quote]
ljust( s, width)
rjust( s, width)
center( s, width)
These functions respectively left-justify, right-justify and center a string
in a field of given width. They return a string that is at least width
characters wide, created by padding the string s with spaces until the given
width on the right, left or both sides. The string is never truncated.

zfill( s, width)
Pad a numeric string on the left with zero digits until the given width is
reached. Strings starting with a sign are handled correctly.
[/quote]

These are from
http://www.python.org/doc/current/lib/module-string.html



-----Original Message-----
From: frpythoneers-bounces at lists.community.tummy.com
[mailto:frpythoneers-bounces at lists.community.tummy.com]On Behalf Of
Brian Jarrett
Sent: Monday, January 26, 2004 9:23 AM
To: FRPythoneers (E-mail)
Subject: [FRPythoneers] dynamically created format strings?


Hello all,

I'm just starting a project with Python where I have to take data from a
database for each student in the school district and construct one long
string (188 chars) for each student that is then placed in a file and sent
to the state.

I'm looking to modularize my code because this is the second project of this
type, so I'm wondering if there is a way to make format strings more
dynamic.  I would like to be able to pass a function some data and then an
integer indicating how many chars it should take up, and possible how it
would be justified.  For instance, first, middle, and last names are
supposed to take up 30 chars each, be left justified, and padded with
spaces.  The resulting format string would be "%-30s"

iRight now I have a separate function created for each name and just pass it
the data.  Also, I have do deal with alot of numbers, so I use format
strings in separate functions like "%02d" and "%010d" to zero-pad numbers.
I'd like to just to be able to pass the number in and an integer
representing how many digits should be used.

Does this make sense?

Brian
_______________________________________________
This message sent by the FRPythoneers mailing list.
Unsubscribe: echo unsubscribe |
FRPythoneers-request at lists.community.tummy.com
URL: http://lists.community.tummy.com/mailman/listinfo/frpythoneers




More information about the FRPythoneers mailing list