HorstPeter
HorstPeter

Reputation: 27

How do I store a large int in python without the newlines being counted by len()?

So I wanna store a long integer which is too big for one line in python. Do I just ignore PEP 8 and just make it longer than 120 characters? Cause if I do it like this:

num="""7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843
8586156078911294949545950173795833195285320880551112540698747158523863050715693290963295227443043557
6689664895044524452316173185640309871112172238311362229893423380308135336276614282806444486645238749
3035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776
6572733300105336788122023542180975125454059475224352584907711670556013604839586446706324415722155397
5369781797784617406495514929086256932197846862248283972241375657056057490261407972968652414535100474
8216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586
1786645835912456652947654568284891288314260769004224219022671055626321111109370544217506941658960408
0719840385096245544436298123098787992724428490918884580156166097919133875499200524063689912560717606
0588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450"""

and try to access a specific index of that integer or use len() on it I get a length of 1009 instead of the 1000 digits the number actually has. And putting everything into one line would make that line 1004 characters long which doesn't seem that great either.

Upvotes: 2

Views: 79

Answers (5)

Enthus3d
Enthus3d

Reputation: 2165

There's lots of good answers already, but here's one that will give you a bit of extra convenience. You just have to put in a number and the size of the chunks per line, and you can reuse it for lots of long numbers, if needed:

Format your number into multiple strings using a for loop and string concatenation:

x = str(7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450)
y = []
y.append("long_num = (")
chunksize = 10
for i in range(0, len(x), chunksize ):
    y.append("\t"+"\""+x[i:i+chunksize ]+"\"")
y.append(")")
for part in y:
    print (part)

Outputs the following string that you can use in your code, referencing @blhsing's answer:

long_num = (                                                                                                         
        "7316717653"                                                                                                 
        "1330624919"                                                                                                 
        "2251196744"                                                                                                 
        "2657474235"                                                                                                 
        "5349194934"                                                                                                 
        "9698352031"                                                                                                 
        "2774506326"                                                                                                 
        "2395783180"                                                                                                 
        "1698480186"
        ...
) ```

Upvotes: 0

wjandrea
wjandrea

Reputation: 32997

I wouldn't use this personally, but one option is to remove the newlines:

num = """
123
456
""".replace('\n', '')

print(repr(num)) # -> '123456'

Upvotes: 0

blhsing
blhsing

Reputation: 106598

I would use the following literal over multiple lines in parentheses for cleanliness:

num = (
    '7316717653'
    '1330624919'
    '2251196744'
)

so that len(num) from the above example returns: 30

Upvotes: 3

nexi
nexi

Reputation: 9

You can take a look at this post Is there a way to implement methods like __len__ or __eq__ as classmethods?

Simple make a class for your long integer, and replace the len(self) function to not count \n

Upvotes: -2

moltarze
moltarze

Reputation: 1501

Another option you have is to put the number into another file (say number.txt) and read it at runtime:

number.txt

7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450

main.py

with open("number.txt", "r") as f:
    number = f.read()

Upvotes: 2

Related Questions