1

I'm doing math operations in Python 3.7. These math operations have digit strings of 10k+. When I copy and paste these digits strings into Python 3.7, they get cut off. Thus, I'm not able to do the math operations.

format(y,'o')

**Note:** “y” is a digit string of 10k+

Is there a way to bypass this character limit in Python 3.7?

Note: This is an issue of a string or character limitation. This is not an issue of handling large numbers. These issues positively correlate. But, they're still fundamentally different. Thus, this isn't a duplicate topic.

edit 2:

I got this to work thanks to Andrew F

if 1:
 from pathlib import Path
 y = Path(r"C:\Users\l\Desktop\y.txt").read_text()
 y = int(y)
 format(y,'o')

**note:** “y.txt” contains only the digit string
abcjme
  • 171
  • 7
  • Since you're sure it's not a duplicate, have you tried reading from a file instead of copy-paste? the clipboard has limits too – iAmTryingOK May 15 '19 at 07:05
  • 2
    8191 characters limit for Windows - see https://stackoverflow.com/questions/3205027/maximum-length-of-command-line-string – Andrew Allen May 15 '19 at 07:11
  • @iAmTryingOK ? how do i get python to read this operation from a file – abcjme May 15 '19 at 07:11
  • @Aran-Fey https://www.dropbox.com/s/7lgpapribpv4dus/python-limit.mp4?dl=0 – abcjme May 15 '19 at 07:22
  • Hmm, that's weird. I'd be surprised if it was the python interpreter's fault, though. It's probably some weird Windows behavior. Just to make sure, does it work if you open a terminal and start python in that terminal? – Aran-Fey May 15 '19 at 07:29
  • @Aran-Fey ·ya, it must be the 8191 char limit of windows · i wonder if getting python to read the operation from a file could bypass this limit · but i don't know how to get python to do this – abcjme May 15 '19 at 07:39
  • 2
    Try reading it from a txt file. Take a look at [this](https://www.w3schools.com/python/python_file_open.asp). – Daniel Legut May 15 '19 at 08:09

1 Answers1

0

If there are new lines in your string, you can paste it into the console as part of a block quote, using """ to mark the beginning and end. As long as each line is under the limit of your system's console (apparently Windows at ~8000) then you should be alright.

Edit: reading data from a file is more or less guaranteed to work, regardless of limitations of your system's console. You can do it with pathlib (part of the standard library in Python 3) or pathlib2 if you're using python 2

from pathlib import Path
y = Path('path/to/your/file.txt').read_text()

Edit 2: here is a complete example of how you can read this from a text file.

Suppose you have a gigantic number (e.g. a googol) and you put it in a text file named gigantic-number.txt. The contents of gigantic-number.txt look like

10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

Within a Python console, you can load the contents of that file and then cast it as an integer. Using the example from before with pathlib

>>> from pathlib import Path
>>> # read the contents of the file as a string
>>> y = Path('gigantic-number.txt').read_text()
>>> # convert the string into an integer, this will fail unless it's actually a number
>>> y = int(y)

Some sources of gigantic numbers produce text files that contain line breaks in them. This makes them more readable in simple text editors like Notepad. One example is from GIMPS: M3217.txt looks like

2591170860132026277762467679224415309418188875531254273039749231618740192665863620862012095168004834
0655069524173319417744168950923880701741037770959751204231306662408291635351795231118615486226560454
7691127595848775610568757931191017711408826252153849035830401185072116424747461823031471398340229288
0745456779079410372882358207058923510684338829868886166586502809276920803396058693087905004095037098
7590211901837199162099400256893511313654882973911265679730324198651725011641270350970542777347797234
9821676443446668383119322540099648994051790241624056519054483690809616061625743042361721863339415852
4264312087372665919620617535357488928945996291951830826218608534009379328394202618665861425032514507
7309627423537682293864940712770084607712421182308080413929808705750471382526457144837937112503208182
6126566649084251699453951887789613650248405739378594599444335231188280123660406262468609212150349937
584782292237144339628858485938215738821232393687046160677362909315071

Building on the example from before, you can read this from a file, remove the whitespace, and then cast as an integer.

>>> from pathlib import Path
>>> from string import digits
>>> # read from file as a string
>>> y = Path('M3217.txt').read_text()
>>> # keep only digit characters and "."
>>> y = ''.join(digit for digit in y if digit in digits+".")
>>> # cast as an integer
>>> int(y)
259117086013202627776246767922441530941...

To be clear, only the number goes in the text file. Your edited question makes it seem like you pasted the Python code into the text file as well which is not going to solve this problem.

Andrew F
  • 2,690
  • 1
  • 14
  • 25
  • · i tried reading data from a file · it didn't work · i edited my post to show this · i've yet to try the block quote method though – abcjme May 15 '19 at 09:23
  • · ah, i was missing y = int(y) · this was the problem · i got it to work now · thanks – abcjme May 16 '19 at 01:24