Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
fixed width numbers
#1
is there an easy way to output numbers in a simple print() call in a fixed width? for cases with leading zeros i have been doing:
print(str(number+1000000)[1:])
but now i want to have leading spaces. do i need to write a function to do this?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
fstring:
>>> zz = 45678
>>> yy = 345
>>> print(f'{zz:10}{yy:10}')
     45678       345
Reply
#3
How are you getting leading zeroes anyway?

For leading spaces you can use either append to your string or make use of more than one argument to your print function.

print( 1*' ' + str(number+1000000)[1:] )
Reply
#4
i am getting leading zeroes by adding a big enough power of 10 and slicing off the leading '1'. i suppose this might work:
print((width*' '+str(number))[-width:])
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#5
(May-26-2019, 05:41 AM)Nwb Wrote: How are you getting leading zeroes anyway?

There is string method .zfill for this:

>>> help(str.zfill)
zfill(self, width, /)
    Pad a numeric string with zeros on the left, to fill a field of the given width.
    
    The string is never truncated.
(END)
>>> str(42).zfill(5)
'00042'
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#6
Ah okay so you want to add leading zeroes.

You can use string formatting to do this very easily.
print(f'{number:0(length)}')

eg:
>>> print(f'{10:020}')
00000000000000000010
Reply
#7
(May-26-2019, 01:13 AM)Skaperen Wrote: is there an easy way to output numbers in a simple print() call in a fixed width?

I don't know whether it qualifies as 'easy way', but there is str.rjust (also str.ljust)

>>> help(str.rjust)
Help on method_descriptor:

rjust(self, width, fillchar=' ', /)
    Return a right-justified string of length width.
    
    Padding is done using the specified fill character (default is a space).
(END)
>>> '42'.rjust(5)
'   42'
>>> '42'.rjust(5,'*')
'***42'
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#8
(May-26-2019, 08:16 AM)Nwb Wrote: Ah okay so you want to add leading zeroes.

You can use string formatting to do this very easily.
print(f'{number:0(length)}')

eg:
>>> print(f'{10:020}')
00000000000000000010
read my first post. i have been doing leading zeroes. now i want to do leading spaces.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#9
Yeah I misread. See what Larz60+ wrote. That's the best way to do it.
Reply
#10
i have a couple ways to do it, now.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python code to set column width 1418 11 1,170 Jan-20-2024, 07:20 AM
Last Post: Pedroski55
  How to do "fixed size" (wrapping) math in Python? AlexanderWulf 13 1,849 Jul-19-2023, 04:13 PM
Last Post: deanhystad
  Fixed colum width for rowLabels i Matplotlib pandabay 0 422 Jun-10-2023, 03:40 PM
Last Post: pandabay
  Encrypt and decrypt in python using own fixed key SriRajesh 3 4,808 Feb-20-2022, 01:18 PM
Last Post: dboxall123
  width of Unicode character Skaperen 6 2,705 Sep-27-2021, 12:41 AM
Last Post: Skaperen
  image.thumbnail(width, height) not working PCesarano 2 3,406 Apr-08-2021, 06:09 PM
Last Post: PCesarano
  Play fixed frequency sound in python 3 jpezz 2 2,763 Feb-07-2021, 08:21 PM
Last Post: jpezz
  Referencing a fixed cell Mark17 2 2,056 Dec-17-2020, 07:14 PM
Last Post: Mark17
  How can I get the width of a string in Python? aquerci 14 16,117 May-27-2019, 06:00 PM
Last Post: heiner55
  Print Numbers starting at 1 vertically with separator for output numbers Pleiades 3 3,720 May-09-2019, 12:19 PM
Last Post: Pleiades

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020