Skip to content

Inconsistent format length while adding separator with underscore #104849

Closed as not planned
@Nabav

Description

@Nabav

Bug report

The f-string allows different lengths and zero padding and adding _ separators (every 3 digits for decimal and every 4 digits for binary and hexadecimal). The length of the generated string with different lengths is inconsistent.

See compiler explorer outputs: https://godbolt.org/z/dv7br5EzG

x = 1
for l in [4, 5, 6, 7, 8]:
    print(f'Length: {l}')
    print(f'  bin: {x:0{l}_b}')
    print(f'  hex: {x:0{l}_x}')
    print(f'  dec: {x:0{l}_}')

Output:

Length: 4
  bin: 0001
  hex: 0001
  dec: 0_001
Length: 5
  bin: 0_0001
  hex: 0_0001
  dec: 0_001
Length: 6
  bin: 0_0001
  hex: 0_0001
  dec: 00_001
Length: 7
  bin: 00_0001
  hex: 00_0001
  dec: 000_001
Length: 8
  bin: 000_0001
  hex: 000_0001
  dec: 0_000_001

Your environment

  • CPython versions tested on: 3.9 and 3.11
  • Operating system and architecture: Debian and compiler explorer

I would expect that the string is first padded (with zeros) to the specified length and then separator characters are inserted as much as needed. Correct me, if I am wrong.

Metadata

Metadata

Assignees

No one assigned

    Labels

    pendingThe issue will be closed if no feedback is provided

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions