Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #197567 > unrolled thread
| Started by | Lawrence D’Oliveiro <ldo@nz.invalid> |
|---|---|
| First post | 2025-10-19 07:26 +0000 |
| Last post | 2025-10-24 06:38 -0400 |
| Articles | 6 — 5 participants |
Back to article view | Back to comp.lang.python
Formatted Integer With Specified Number Of Digits Lawrence D’Oliveiro <ldo@nz.invalid> - 2025-10-19 07:26 +0000
Re: Formatted Integer With Specified Number Of Digits Gilmeh Serda <gilmeh.serda@nothing.here.invalid> - 2025-10-19 09:17 +0000
Re: Formatted Integer With Specified Number Of Digits songbird <songbird@anthive.com> - 2025-10-19 10:01 -0400
Re: Formatted Integer With Specified Number Of Digits Lawrence D’Oliveiro <ldo@nz.invalid> - 2025-10-19 21:39 +0000
Re: Formatted Integer With Specified Number Of Digits Paul Rubin <no.email@nospam.invalid> - 2025-10-24 01:14 -0700
Re: Formatted Integer With Specified Number Of Digits Alan Bawden <alan@csail.mit.edu> - 2025-10-24 06:38 -0400
| From | Lawrence D’Oliveiro <ldo@nz.invalid> |
|---|---|
| Date | 2025-10-19 07:26 +0000 |
| Subject | Formatted Integer With Specified Number Of Digits |
| Message-ID | <10d23rk$2bc2b$1@dont-email.me> |
Formatting an integer with 3 digits, excluding base specifier:
>>> "%#0.3x" % 2
'0x002'
No equivalent to this in any of the other ways that Python allows for
formatting:
>>> format(2, "#03x")
'0x2'
(Not what I want)
>>> format(2, "#0.3x")
Traceback (most recent call last):
File "<python-input-10>", line 1, in <module>
format(2, "#0.3x")
~~~~~~^^^^^^^^^^^^
ValueError: Precision not allowed in integer format specifier
>>> "{:#03x}".format(2)
'0x2'
(Not what I want)
>>> "{:#0.3x}".format(2)
Traceback (most recent call last):
File "<python-input-13>", line 1, in <module>
"{:#0.3x}".format(2)
~~~~~~~~~~~~~~~~~^^^
ValueError: Precision not allowed in integer format specifier
Why not?
[toc] | [next] | [standalone]
| From | Gilmeh Serda <gilmeh.serda@nothing.here.invalid> |
|---|---|
| Date | 2025-10-19 09:17 +0000 |
| Message-ID | <k02JQ.19870$UXx1.12020@fx17.ams4> |
| In reply to | #197567 |
On Sun, 19 Oct 2025 07:26:44 -0000 (UTC), Lawrence D’Oliveiro wrote:
> Why not?
I have no idea
you could try zfill:
>>> f"magic number: {'42'.zfill(4)}"
'magic number: 0042'
>>> f"magic number: {str(42).zfill(4)}"
'magic number: 0042'
more info:
https://fstring.help/cheat/
--
Gilmeh
Necessity is a mother.
[toc] | [prev] | [next] | [standalone]
| From | songbird <songbird@anthive.com> |
|---|---|
| Date | 2025-10-19 10:01 -0400 |
| Message-ID | <ci7fsl-dj2.ln1@anthive.com> |
| In reply to | #197567 |
Lawrence D’Oliveiro wrote:
> Formatting an integer with 3 digits, excluding base specifier:
>
> >>> "%#0.3x" % 2
> '0x002'
>
> No equivalent to this in any of the other ways that Python allows for
> formatting:
>
> >>> format(2, "#03x")
> '0x2'
>
> (Not what I want)
>
> >>> format(2, "#0.3x")
> Traceback (most recent call last):
> File "<python-input-10>", line 1, in <module>
> format(2, "#0.3x")
> ~~~~~~^^^^^^^^^^^^
> ValueError: Precision not allowed in integer format specifier
>
> >>> "{:#03x}".format(2)
> '0x2'
>
> (Not what I want)
>
> >>> "{:#0.3x}".format(2)
> Traceback (most recent call last):
> File "<python-input-13>", line 1, in <module>
> "{:#0.3x}".format(2)
> ~~~~~~~~~~~~~~~~~^^^
> ValueError: Precision not allowed in integer format specifier
>
> Why not?
https://github.com/tpauldike/printf
songbird
[toc] | [prev] | [next] | [standalone]
| From | Lawrence D’Oliveiro <ldo@nz.invalid> |
|---|---|
| Date | 2025-10-19 21:39 +0000 |
| Message-ID | <10d3lq4$2prhm$2@dont-email.me> |
| In reply to | #197569 |
On Sun, 19 Oct 2025 10:01:16 -0400, songbird wrote: > Lawrence D’Oliveiro wrote: >> >> Formatting an integer with 3 digits, excluding base specifier: >> >> >>> "%#0.3x" % 2 >> '0x002' >> >> No equivalent to this in any of the other ways that Python allows for >> formatting: >> >> Why not? > We already have printf-style formatting in Python, that’s what I was using above.
[toc] | [prev] | [next] | [standalone]
| From | Paul Rubin <no.email@nospam.invalid> |
|---|---|
| Date | 2025-10-24 01:14 -0700 |
| Message-ID | <87ikg467io.fsf@nightsong.com> |
| In reply to | #197567 |
Lawrence D’Oliveiro <ldo@nz.invalid> writes:
> >>> "%#0.3x" % 2
> '0x002'
f'0x{2:03x}
[toc] | [prev] | [next] | [standalone]
| From | Alan Bawden <alan@csail.mit.edu> |
|---|---|
| Date | 2025-10-24 06:38 -0400 |
| Message-ID | <86ldl037qq.fsf@williamsburg.bawden.org> |
| In reply to | #197571 |
Paul Rubin <no.email@nospam.invalid> writes:
Lawrence D’Oliveiro <ldo@nz.invalid> writes:
> >>> "%#0.3x" % 2
> '0x002'
f'0x{2:03x}
Won't work for negative numbers.
--
Alan Bawden
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web