469,588 Members | 2,688 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,588 developers. It's quick & easy.

formatting strings to have the same width

hi,

i got random strings and wanna attach a " | " at the end. now if i
print them i want the | to always be underneath each other. example
code:

foo = ["aaa", "1232"]
for each in foo:

print foo[0].center(10, " ") + " | "
foo2 = "1232"
print foo2.center(10, " ") + " | "

even though i define a constant width on the strings the | don't show
up underneath each other.
any ideas ?

Mar 17 '07 #1
3 1493
sorry the code should read:

foo = ["aaa", "1232"]
for each in foo:
print each.center(10, " ") + " | "

Mar 17 '07 #2
On Sat, 17 Mar 2007 16:07:44 -0700, spohle wrote:
hi,

i got random strings and wanna attach a " | " at the end. now if i
print them i want the | to always be underneath each other. example
code:

foo = ["aaa", "1232"]
for each in foo:

print foo[0].center(10, " ") + " | "
foo2 = "1232"
print foo2.center(10, " ") + " | "

even though i define a constant width on the strings the | don't show
up underneath each other.
any ideas ?
Three possibilities:

(1) Your code is fine, but whatever you are using to display the strings
is wrong because it is using the wrong sort of font.

What font are you using? If it is a proportional font, e.g. Times New
Roman, then each different character is a different width, and counting
characters is not likely to result in equal widths.

e.g. compare these two lines:

mw mw mw mw mw |
il il il il il |

If the vertical bars line up, you're (probably) using a fixed-width font
like Courier. If they're not, you're (probably) using a proportional font
like Ariel or Times.
(2) Your code is fine, but your data is bad.

You are centering your strings with a width of ten. Is it possible that
some of the strings you are passing into the are longer than ten
characters? If so, you'll get funny results, because str.center() will
never shorten the string you pass it.
(3) You code in Python like you write in English: poorly with lots of
errors. "Wanna" isn't a word (unless you're trying to be funny), and
capitalization is the difference between:

I helped my Uncle Jack off a horse

and

I helped my uncle jack off a horse.

After fixing your indentation error, your example code works perfectly
for me. Perhaps your real code is different, and part of the difference
causes the problem? For instance, your example code seems to do something
unlikely to be useful:

foo = ["aaa", "1232"]
for each in foo:
print foo[0].center(10, " ") + " | "

That prints the FIRST item each time around the loop, instead of each item.

Also, you don't need the second argument to center. By default, space is
used as a fill character.

--
Steven.

Mar 17 '07 #3
On Mar 18, 10:17 am, "spohle" <spo...@gmail.comwrote:
sorry the code should read:

foo = ["aaa", "1232"]
for each in foo:
print each.center(10, " ") + " | "
Works OK for me on the console:

Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit
(Intel)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>foo = ['123', '1234', '12345']
for each in foo:
.... print each.center(10, ".") + " | "
....
....123.... |
....1234... |
...12345... |
>>>
for each in foo:
.... print each.center(10, " ") + " | "
....
123 |
1234 |
12345 |
>>for each in foo:
.... guff = each.center(10, " ") + " | "
.... print guff + str(len(guff))
....
123 | 13
1234 | 13
12345 | 13
>>>
Of course if I go into IDLE and change it to use a proportionally
spaced font (e.g. Arial), and do this:
>>foo = ['lllll', 'mmmmm']
for each in foo:
print each.center(10, ' ') + ' | '
lllll |
mmmmm |
>>>
naturally they don't line up. If something like that isn't the cause
of your problem, you'll need to provide more information about your
environment.

HTH,
John

Mar 18 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

11 posts views Thread by Steve Holden | last post: by
4 posts views Thread by Michael Hill | last post: by
20 posts views Thread by Andy Fish | last post: by
4 posts views Thread by Richard | last post: by
2 posts views Thread by David Veeneman | last post: by
6 posts views Thread by Tomasz J | last post: by
2 posts views Thread by Jean-Paul Calderone | last post: by
reply views Thread by suresh191 | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.