473,320 Members | 1,990 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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 1604
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Steve Holden | last post by:
I was messing about with formatting and realized that the right kind of object could quite easily tell me exactly what accesses are made to the mapping in a string % mapping operation. This is a...
4
by: Michael Hill | last post by:
I am having a problem formatting the third column. It should be 70px wide, but it is extending well beyond 70. The 2nd and 3rd column should be the same width. The content of the 3rd column...
20
by: Andy Fish | last post by:
Hi All, I have a nice easy table and/or CSS formatting problem for any gurus out there. I have a table with 4 columns - no "width" attribute on anything. Some rows have 4 cells in (call...
4
by: Richard | last post by:
I am new to C++ and I do not understand the formatting code very well. I need to format my output to look like this: Movie Name: "Death Grip" Adult Tickets Sold: ...
3
by: Jouke Langhout | last post by:
Hello all! For quite some time now, I've got the following problem: Access won't close properly when a user closes the application. An ACCESS process stays active and that process can only be...
10
by: Coleen | last post by:
Hi all :-) I have a weird formatting problem with an HTML table that I am populating using VB.Net and HTML. Here is the snippet of code for the cell I'm trying to format: Dim...
2
by: David Veeneman | last post by:
How does one format a date column in a GridView control? I had assumed that the DataFormat string would do it, but MSDN only shows numeric formatting codes. Can dates be formatted using that...
6
by: Tomasz J | last post by:
Hello developers, I bind my TextBox control specyfying a format stored in my application global ApplicationContext object - it has a static string CurrencyFormat property. The problem - this...
2
by: Jean-Paul Calderone | last post by:
On Fri, 5 Sep 2008 14:24:16 -0500, Robert Dailey <rcdailey@gmail.comwrote: mystring = ( "This is a very long string that " "spans multiple lines and does " "not include line breaks or tabs "...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.