Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Format String
#1
Hey,

I try to print some formatted integers with padded 0.

But there is always a blank character in front of the print.

My code is:
num = 14
print(f"{num: 010d}")
And my output is: 000000014
There are 9 digits and a blank character. Why is there a blank char and not only 10 digits?


THX
Reply
#2
The space is there because you have a space in your format string. The space is what you should print as the sign for a positive number.
num = 14
print(f"{num:010d}")  # No prefix for positive numbers
print(f"{-num:010d}")
print(f"{num:+010d}") # + prefix for positive numbers
print(f"{-num:+010d}")
print(f"{num: 010d}")  # space prefix for positive numbers
print(f"{-num: 010d}")
Output:
0000000014 -000000014 +000000014 -000000014 000000014 -000000014
Reply
#3
Oh, ok. Thanks for your answer.
I didn't know, that the space between ':' and '0' counts.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Set string in custom format korenron 4 1,083 Jan-16-2023, 07:46 PM
Last Post: mutantGOD
  TypeError: not enough arguments for format string MaartenRo 6 2,919 Jan-09-2022, 06:46 PM
Last Post: ibreeden
  string format challenge jfc 2 1,777 Oct-23-2021, 10:30 AM
Last Post: ibreeden
  Print first day of the week as string in date format MyerzzD 2 2,011 Sep-29-2021, 06:43 AM
Last Post: MyerzzD
  string.format() suddenly causing errors with google drive API zwitrader 0 1,756 Jun-28-2021, 11:38 PM
Last Post: zwitrader
  String to Date format SAF 2 2,448 Apr-06-2021, 02:09 PM
Last Post: snippsat
  MySQLdb._exceptions.ProgrammingError: not enough arguments for format string. farah97 0 3,328 Jan-22-2020, 03:49 AM
Last Post: farah97
  Highlight/Underline a string | ValueError: zero length field name in format searching1 1 3,017 Jul-01-2019, 03:06 AM
Last Post: metulburr
  write image into string format into text file venkat18 2 4,402 Jun-01-2019, 06:46 AM
Last Post: venkat18
  String format checking? MuntyScruntfundle 1 2,430 Mar-06-2019, 12:01 PM
Last Post: buran

Forum Jump:

User Panel Messages

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