print formatting

V

vsoler

Hello,

My script contains a print statement:

print '%40s %15d' % (k, m)

However,

1- the string is right adjusted, and I would like it left
adjusted
2- the number is a decimal number, and I would like it with
the thousands separator and 2 decimals

If possible, the thousands separator and the decimal separator should
use my local settings.

Is there any way to achieve this?
 
S

Steve Holden

vsoler said:
Hello,

My script contains a print statement:

print '%40s %15d' % (k, m)

However,

1- the string is right adjusted, and I would like it left
adjusted
2- the number is a decimal number, and I would like it with
the thousands separator and 2 decimals

If possible, the thousands separator and the decimal separator should
use my local settings.

Is there any way to achieve this?

Left-alignment is achieved by using a negative width.

You can use the locale module to generate thousands-separated numeric
string representations:
' 123,456.789'

regards
Steve
 
P

Peter Otten

vsoler said:
My script contains a print statement:

print '%40s %15d' % (k, m)

However,

1- the string is right adjusted, and I would like it left
adjusted
2- the number is a decimal number, and I would like it with
the thousands separator and 2 decimals

If possible, the thousands separator and the decimal separator should
use my local settings.

Is there any way to achieve this? 'de_DE.UTF-8'

Traditional:
hello | 1.234.567

New:'hello 1.234.567'

See also:
http://docs.python.org/dev/py3k/library/string.html#formatstrings

Peter
 

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,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top