Sun Bear
Sun Bear

Reputation: 8234

How to print a long string of numbers neatly in python 3.6?

>>> text = [ str(i) for i in range(1, 100)]
>>> print( " {}".format( ", ".join( str(i) for i in text ) ) )
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99

Instead of printing a long text string, I would like a neater print out of text with 80 characters width for each line, each line is indented with 2 character space and each element to be equally spaced like so:

   1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 
  21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
  41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60,
  61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80,
  81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99

Is there a ready python object that I can use to do this? If not, what is the pythonic way of achieving this in python 3.6?

Upvotes: 3

Views: 847

Answers (5)

Hai Vu
Hai Vu

Reputation: 40688

As Saelyth pointed out, use textwrap to format this, but before that, you need to format each number right justified and take up 2 spaces:

import textwrap

text = [str(i) for i in range(1, 100)]
one_line = ', '.join('{:>2}'.format(e) for e in text)
# one_line == ' 1,  2,  3, ...'
print('\n'.join(textwrap.wrap(one_line, width=80)))

Output:

 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60,
61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80,
81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99

Update

If you want 2-space indent, the first way is to print each line with the indentation:

import textwrap

text = [str(i) for i in range(1, 100)]
one_line = ', '.join('{:>2}'.format(e) for e in text)
# one_line == ' 1,  2,  3, ...'
for line in textwrap.wrap(one_line, width=78):
    print('  {}'.format(line))

The second way is to use textwrap.indent:

import textwrap

text = [str(i) for i in range(1, 100)]
one_line = ', '.join('{:>2}'.format(e) for e in text)
# one_line == ' 1,  2,  3, ...'
block = '\n'.join(textwrap.wrap(one_line, width=78))
print(textwrap.indent(block, '  '))

Note that because of the 2-space indentation, I reduced the width from 80 down to 78.

Update 2

Thanks to Tomerikoo for the suggestion to use textwrap.fill, the code is now simpler:

import textwrap

text = [str(i) for i in range(1, 100)]
one_line = ', '.join('{:>2}'.format(e) for e in text)
# one_line == ' 1,  2,  3, ...'

print(textwrap.fill(one_line,
                    width=80,
                    initial_indent='  ',
                    subsequent_indent='  '))

Update 3

In this updated, I added advice from Tim to reduce the steps further.

import textwrap

one_line = ', '.join('{:>2}'.format(e) for e in range(1, 100))
# one_line == ' 1,  2,  3, ...'

print(textwrap.fill(one_line,
                    width=80,
                    initial_indent='  ',
                    subsequent_indent='  '))

Upvotes: 6

Tim
Tim

Reputation: 2637

Many of the other answers are missing the power of the Python formatting mini-language also, as you are using Python 3.6 f-strings!

# Format range into a spaced-out string
formatted_range = ", ".join(f"{x:2d}" for x in range(0, 100))

# Split into lines and print
print(textwrap.fill(formatted_range, width=80))

f{x:2d} will format an integer to take up 2 characters (padded with spaces). More info here

Again textwrap is used to split across lines, I have used textwrap.fill which is a shorthand for "\n".join(wrap(text, ...))

Upvotes: 2

Tomerikoo
Tomerikoo

Reputation: 19414

Using only format. We first pack all the elements to one long line while fixing their length to 2. Then just joining the lines with '\n' using cs_in_line jumps with added 2-spaces:

text = [str(i) for i in range(1, 100)]

one_line = ", ".join("{:>2}".format(num) for num in text)

print("\n".join("  "+one_line[i:i+cs_in_line] for i in range(0, len(one_line), cs_in_line)))

And this gives:

   1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 
  21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 
  41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 
  61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 
  81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99

Upvotes: 1

Saelyth
Saelyth

Reputation: 1734

import textwrap
maximum = 100
text = str([str(i).zfill(len(str(maximum))) for i in range(1, maximum)])[1:-1]
text = textwrap.wrap(text, width=20)
for i in text:
    print(i)

This would do the trick, the textwrap module makes "lists" of your desired width in case you want to work with those lines. So you just need to feed it some text and get your result.

Also added zfill so all numbers are of the same lenght as the maximum number.

If you just need the text, other answers are better.

Upvotes: 1

moltarze
moltarze

Reputation: 1501

From the textwrap library:

>>> text = [str(i) for i in range(1, 100)]
>>> print(textwrap.fill(" {}".format(", ".join(str(i) for i in text)), width=80))

The description of textwrap.fill:

textwrap.fill(text, width=70, **kwargs)

Wraps the single paragraph in text, and returns a single string containing the wrapped paragraph. fill() is shorthand for

"\n".join(wrap(text, ...))

Basically this function wraps the text to the given width. If you are willing to accept the default width of 70, you can ommit the width keyword argument entirely.

Upvotes: 2

Related Questions