1

so far this is what i found:

 from random import randint
 randint(100000000000000000000, 999999999999999999999)

the output is:

922106555361958347898L

but i do not want that L there..

i can only use this as an int if there is no "L" there at the end of it.

UPDATE

would it be a better idea to generate two small numbers and then combine them if the goal is to simply have a random number that is 30 digits long ?

  • 1
    Don't print the `repr()` of the value and you're fine. What are you doing with that value? – Wooble Sep 08 '14 at 17:10
  • 1
    Any integer outside the range of +/-2**31 won't fit in small integer object, and your numbers are much larger. – Mark Ransom Sep 08 '14 at 17:12
  • 2
    possible duplicate of [Why do integers in database row tuple have an 'L' suffix?](http://stackoverflow.com/questions/11764713/why-do-integers-in-database-row-tuple-have-an-l-suffix) – Schorsch Sep 08 '14 at 17:13
  • @Schorsch: I'm not sure whether it's a dup or not. Clearly the two askers had similar confusion, but I'm not sure the answers there would help here. (But if nothing else, it _definitely_ deserves to appear in the Linked questions list…) – abarnert Sep 08 '14 at 17:25
  • 1
    "would it be a better idea to generate two small numbers and then combine them if the goal is to simply have a random number that is 30 digits long ?" No. A random number that's 30 digits long is going to be a `long`, no matter how you create it, because you can't fit 30 digits in an `int`. And there's nothing wrong with that. Trying to combine two smaller numbers is just going to add complexity, inefficiency, and an easy place for bugs to slip in, for no benefit. – abarnert Sep 08 '14 at 17:26
  • Just use `str(randint(100000000000000000000, 999999999999999999999))` to output the value. – martineau Sep 08 '14 at 17:38
  • @abarnert it is not an exact duplicate. but the question where the `L` comes from and why it is there is answered in the other question. – Schorsch Sep 08 '14 at 17:39
  • @user4018347: If your goal is to insert it into MySQL… well, IIRC, the two most popular MySQL libraries for Python 2.x convert all integer values into `long` anwyay, even if they're small enough to fit in an `int`, so your problem is even more not a problem. – abarnert Sep 08 '14 at 17:40
  • Actually, scratch that; [`MySQLdb`](http://mysql-python.sourceforge.net/MySQLdb.html) apparently converts signed `INTEGER`, `INT`, and smaller types into `int`, and anything larger or unsigned into `long`. But that doesn't matter; it'll still take `long` values for `TINYINT` columns and `int` values for `BIGINT` columns without complaint. So the point still stands: you don't have to worry about it. – abarnert Sep 08 '14 at 17:49
  • @abarnert sorry, what I meant was either `2**31 - 1` or to specify that the range was non-inclusive of the endpoints. – Mark Ransom Sep 08 '14 at 18:07

3 Answers3

9

The reason there's an L there is because this is too large to fit into an int,* so it's a long. See Numeric Types — int, long, float, complex in the docs for more details.


So, why do you get that L, and how do you get rid of it?

Python has two different ways to turn a value into a string representation:

  • repr either returns the canonical source-code representation, or something like <__main__.Eggs at 0x10fbd8908>).
  • str returns a human-friendly representation.

For example, for strings, str('abc') is abc, while repr('abc') is 'abc'.

And for longs, str(1L) is 1, while repr(1L) is 1L.


When you just type an expression at the interactive prompt, it uses repr. But when you use the print command, it uses str. So, if you want to see the value without the L, just print it:

print randint(100000000000000000000, 999999999999999999999)

If you want to, e.g., save the string in a variable or write it to a file, you have to call str explicitly.

But if you just want to use it as a number, you don't have to worry about this at all; it's a number, and int and long values can be intermixed freely (as of Python 2.3 or so).

And if you're trying to store it in a MySQL database, whichever MySQL interface you use won't care whether you're giving it int values or long, as long as they fit into the column type.**


Or you could upgrade to Python 3.x, where there is no separate long type anymore (all integers are int, no matter how big) and no L suffix.


* The exact cutoff isn't documented anywhere, but at least for CPython, it's whatever fits into a C long on your platform. So, on most 64-bit platforms, the max value is (1<<63)-1; on the other 64-bit platforms, and all 32-bit platforms, it's (1<<31)-1. You can see for yourself on your platform by printing sys.maxint. At any rate, your number takes 70 bits, so unless someone ports Python 2.x to a platform with 128-bit C longs, it won't fit.

** Note that your values are too big to fit into even a MySQL BIGINT, so you're going to be using either DECIMAL or NUMERIC. Depending on which interface you're using, and how you've set things up, you may have to convert to and from strings manually. But you can do that with the str and int functions, without worrying about which values fit into the int type and which don't.)

abarnert
  • 354,177
  • 51
  • 601
  • 671
2

If you're on the interactive prompt, explicitly print the value. The repr of the value has an L, but the str of the value doesn't.

>>> 922106555361958347898
922106555361958347898L
>>> print 922106555361958347898
922106555361958347898
Kevin
  • 74,910
  • 12
  • 133
  • 166
0

The output in the REPL has an L suffixed; if you print the value, it is not displayed.

>>> from random import randint
>>> print randint(100000000000000000000, 999999999999999999999)
106315199286113607384
>>>
chepner
  • 497,756
  • 71
  • 530
  • 681