Quick Python formatting question

OK, this is one I know should be able to be solved easily, despite searching here and there I haven’t found out how…

result="{}".format(040)
print result

result is >>>32 ???

0400 makes 256, etc… So it is automagically interpreting it as octal? How do I get it to just give me the raw string equivalent?

Use format type specifier to force the type you want

https://docs.python.org/2.7/library/string.html#formatspec

And as a refresher the form of numeric literals

https://docs.python.org/2.7/reference/lexical_analysis.html#integer-and-long-integer-literals

Yep, looked in there, still could not figure it out…

…doesn’t make much sense to me either…

Hi Mitch,

Where will the 040 variable come from? I suspect you are retrieving it as a string, correct?
For this works:

print float('040')
print float('040.8')
40.0
40.8

It’s a float, but I need to convert it to a string. This doesn’t work either:

result="{}".format(float(040))
print result

Nor this

result=str(040)
print result

I don’t understand it I think.

to format to a string you can do this:

result="{}".format('040')

What I do not know is where the 040 comes from. Is it a variable and is this an example or is it a hardcoded input? I guess I have a lack of context

-Willem

Yes, it is a variable which is simply a measurement obtained from an object (hence float), and yes, this is just an example. Try it with 0400 or 04000. OTOH, it works fine if there is no leading 0 such as 40, 400, 4000 etc.

How do you get a measurement with a leading 0?

If this is for the cuttinglist cvs exprt, I was having a look at that as well
does this suffice?:

def SF(num):
    #strips leading/trailing zeros plus decimal point from float if possible
    #returns string
    stripped_zero = "{}".format(num).strip("0")
    stripped_point = stripped_zero.strip(".")
    return stripped_point

Oh, I’m just being stubborn, I always try to create a general case assuming that one never knows where the data is coming from… one function that would intelligently strip leading and trailing zeroes but preserve the numbers…

Your above definition doesn’t work for my case because for 400 it returns 4…

The following works fine if I assume no leading zeroes -

def SF(num):
    #strips trailing zeros plus decimal point from float if possible
    #preserves trailing zeros to left of decimal.  returns string
    num_str="{}".format(num)
    if "." in num_str:
        num_parts=num_str.split(".")
        num_str,back=num_parts
        back=back.rstrip("0")
        if back: num_str="{}.{}".format(num_str,back)
    return num_str

I guess I’ll leave at that now.

@Willem - this is what’s odd - its not even the format or str functions that is causing this - check the example below:

n=040
print n, type(n)

32 <type ‘int’>

So I guess an integer with a leading 0 is always interpreted as octal? Does anyone know where this is documented in the Python docs?

From the second link I posted:

octinteger    ::= "0" ("o" | "O") octdigit+ | "0" octdigit+
octdigit      ::= "0"..."7"

Meaning: numerical literal starting with a 0 and followed by at least one octdigit between 0 and 7 inclusive us an octal integer. That is the last part if the first line.

It also could be a capital o followed by at least one octdigit (middle part of first line).

Or it could start with a zero, followed by an either-cased o-letter and then at least one octdigit, making it an octinteger according the first form of the first line.

Try print(090). You’ll get an exception…

Meh, typing on the phone gives for funky explanation, in short octal integers start with one or two zeroes or a zero and an o-letter. After tgat start at least one octal digit in the range 0-7 inclusive.