formatting strings to have the same width

S

spohle

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

spohle

sorry the code should read:

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

Steven D'Aprano

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.
 
J

John Machin

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... |.... print each.center(10, " ") + " | "
....
123 |
1234 |
12345 |.... 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top