سؤال

The issue is that I have an integer variable (e.g. X = 123) that needs to be concatenated with the string (e.g. "L") and still remain an "integer." The reason is that the id I'm passing to an api call needs the id to be in the following format:

10150247729954L 

Notice that because there are no quotes, it is not a string. I'm assuming it's still an integer variable, as it's purple like other numbers in sublime.

For example, how do I turn this integer into the desired format:

integer = 10150247729954 
desired_integer = 10150247729954L

This is what I've tried which has resulted in errors:

 int('10150247881525L')
 ValueError: invalid literal for int() with base 10: '10150247881525L'

When I just try to push the string through the API Call I get:

 TypeError: %d format: a number is required, not str
هل كانت مفيدة؟

المحلول

L is not part of the numeric value.

Rather, in Python 2.x, it is part of a integer literal as understood by the Python parser - it results in a long value vs an int value, if it were not suffixed. (Python will automatically up-promote larger integer literals to long values as required, the L is optional in this case.)

The repr of long will also be suffixed with L, where there is no suffix for int values.

Use long(anInt) to force explicit conversion from int to long.


Python 3 removes the "L" notation as it effectively combines long/int (ref. integer changes).

نصائح أخرى

Basically, you want to convert an integer into a long integer. This can be done with the long built-in:

>>> integer = 10150247729954
>>> long(integer)
10150247729954L
>>>
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top