3

For example I have 100 digit integer, which I want to break into multiple lines for better over view.

x = 1000000000
    0000000000
    0000000000
    0000000000
    0000000000
    0000000000
    0000000000
    0000000000
    0000000000
    0000000000

I tried breaking as per pep8, but it's showing invalid syntax error.

147875
  • 193
  • 5
  • 4
    AFAIK you can't. Python does not allow number breaking. Also, you may want to rethink your design. Such huge constants will never be readable, regardless of line breaking. – freakish Apr 17 '20 at 07:21
  • This [discussion](https://stackoverflow.com/questions/9475241/split-string-every-nth-character) can maybe help you – Alexandre B. Apr 17 '20 at 07:21
  • or you can define your own function like one in this [question](https://stackoverflow.com/questions/18854620/whats-the-best-way-to-split-a-string-into-fixed-length-chunks-and-work-with-the/18854817) – Ait Yahia Idir Apr 17 '20 at 07:24
  • @freakish I am doing a problem in [https://projecteuler.net/](https://projecteuler.net/) where I have to work on 1000 digit integer, though I can split the integer in pieces using string. Writing in single line is hard to visualize. – 147875 Apr 17 '20 at 07:29
  • 2
    You can use scientific notation as discussed [here](https://stackoverflow.com/questions/6913532/display-a-decimal-in-scientific-notation) ? – Alexandre B. Apr 17 '20 at 07:33
  • @AlexandreB. unfortunately you will loose precision when converting it to integer ` >>>int(1e100) 10000000000000000159028911097599180468360808563945281389781327557747838772170381060813469985856815104 ` – user7440787 Apr 17 '20 at 07:35
  • @AlexandreB. But I can't do that, since it does not have any zeros towards end. It is like 1234567890. I'll lose numbers if I user scientific notation – 147875 Apr 17 '20 at 07:36
  • 1
    @JvdV I am not concerned with printing the number, rather breaking single line of long integer into multiple lines – 147875 Apr 17 '20 at 07:42
  • 1
    @JvdV No, I asked this question to know if long integer can be split into multiple line when writing a code, so that I can visualize the number clearly – 147875 Apr 17 '20 at 08:14

3 Answers3

2

You can use write big numbers like this:

x = 10**n (n being the power)

Another form (reacting to your comment) is:

x = 123 x = x * (10**3) + 456 (3 is a form of n)

A45
  • 165
  • 1
  • 8
1

I do not know if it is possible with integers, but you might use / to make multiline str representing number and then convert it to int, i.e.

x = '100\
000\
000'
x = int(x)
print(x)  # 100000000
print(type(x))  # <class 'int'>

(tested in Python 3.7.3)

This naturally means additional operation as opposed to explicit x = 100000000, however that conversion shouldn't be problem, unless your task is ultra-time-critical.

Regarding readibility note that if you use Python 3.6 or newer you might use underscores in numeric literals as PEP 515 says. Speaking simply - you might place _ in numeric literals as long as it is not adjacent to another _ and not leading and not trailing character, so for example all following lines have same effect:

x = 1000
x = 1_000
x = 1_0_0_0

and so on

Daweo
  • 31,313
  • 3
  • 12
  • 25
0

Let:

x = 1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

You could split it and view:

for i in [str(x)[i:i+10] for i in range(0,len(str(x)),10)]:
    print(i)

Gives:

1000000000
0000000000
0000000000
0000000000
0000000000
0000000000
0000000000
0000000000
0000000000
0000000000

As numbers can't be subscribed, we take the string of the number (x remains unaffected).
To store the output as a string ( as a view of x ):

view_of_x = '\n'.join([str(x)[i:i+10] for i in range(0,len(str(x)),10)])

Now print it:

print(view_of_x)
Joshua Varghese
  • 5,082
  • 1
  • 13
  • 34