473,325 Members | 2,771 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,325 software developers and data experts.

py3k - format specifier for strings

Hello.
Python 3.0.a1 has been released. I'm trying to get the hang of the new
string formatting in the form:
>>a = "{0}".format(5.66)
a
'5.66'

There are more options in PEP 3101 (fill, alignment, etc.), but I'm having
trouble implementing them. The doc has not yet been written. I haven't
consulted the source code yet.

Any help in the way of examples would be appreciated.

Carl T.
Sep 4 '07 #1
3 1923
>>>a = "{0}".format(5.66)
>>>a
'5.66'

There are more options in PEP 3101 (fill, alignment, etc.), but I'm having
trouble implementing them.
It would be good if you could describe these troubles in more detail.
What have you been trying, what happened, and what did you expect to
happen instead?
Any help in the way of examples would be appreciated.
From test_unicode.py:

"My name is {0}".format('Fred')
"My name is {0[name]}".format(dict(name='Fred')
"My name is {0} :-{{}}".format('Fred')

d = datetime.date(2007, 8, 18)
"The year is {0.year}".format(d)

'{0:.3s}'.format('abc')
'{0:.0s}'.format('abcdef')
'{0:3.3s}'.format('abc')
'{0:3.2s}'.format('abc')
'{0:x<0s}'.format('result')
'{0:x<7s}'.format('result')
'{0:^10s}'.format('result')
'{0!s}'.format('Hello')
'{0!s:15s}'.format('Hello')
'{0!r:}'.format('Hello')

Regards,
Martin
Sep 4 '07 #2
like format function in Visual Basic,
format("##.##%",0.3456) ==> 34.56%
Sep 4 '07 #3
Ginger wrote:
like format function in Visual Basic,
format("##.##%",0.3456) ==> 34.56%
"%5.2f%%" % (0.3456*100)
'34.56%'
See this section of the manual:
http://docs.python.org/lib/typesseq-strings.html

Gary Herron

Sep 4 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: indushekara | last post by:
Hi, We are having memory corruption in our application somewhere, unable to find out. one part of code we found that we are specifying wrong format specifier. Could anyone let me know if the...
18
by: Money | last post by:
Here in this thread http://groups.google.co.in/group/comp.lang.c/browse_frm/thread/c16d280238a95c9/e3b0dbf76f3e02e3?q=finding+endianness&rnum=1#e3b0dbf76f3e02e3 Tydr Schnubbis in 3rd reply used...
1
by: dis_is_eagle | last post by:
Hi.i dont know exactly how the format specifier %d works.please explain the following program. int main() { int k=5; printf("k=%*d",k,k); return 0; } It gives the output of k= 5.If I omit...
2
by: =?Utf-8?B?Um9ubmllIFNtaXRo?= | last post by:
Hello, I would like to be able to accept a format string in the form of say {0:D2} as an input from a dialog box, and then dynamically format the output string based on this new format...
1
by: raghunadhs | last post by:
hi! This is raghuandhs. i would like to know the format specifier of unsigned long in c? could you please say any body?
7
by: jayapal | last post by:
Hi all, what is %p format specifier indicates in the printf statement? regards, Jay
1
by: dissectcode | last post by:
Hello - I am looking at a scanf function that takes in a 16 bit unsigned int(UINT16), but its format specifier is %hd. According to ...
7
by: codinginc | last post by:
Hi Everyone, Is it possible to insert format specifier in a form of variable. See example below Standard from char result; float fnum = 3.14159; sprintf( result, "%f", fnum );...
3
amarniit
by: amarniit | last post by:
I am making a project on the basis of numbers........here i need that i print a bigger number whose lenght is more than 300 digits....... while i am doing it in C than i am getting a problem my all...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
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: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
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)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
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.