-3

I'm having a hard time finding what can be put inside literal strings.

For example, I've seen this code on the PEP above, yet I didn't find any information above about what it does.

>>> value = 1234
>>> f'input={value:#06x}'
'input=0x04d2'

Is there a tutorial for understanding string literals better?

Yuval Meshorer
  • 156
  • 2
  • 8

1 Answers1

3

What's new here is that you can just literally write value inside the f-string and Python will insert it.

The #06x part is nothing new and just a way to format numbers in hexadecimal representation. Python2:

>>> value = 1234
>>> '{:#06x}'.format(value)
'0x04d2'

# says to prefix the output (here with 0x).
06 says pad left with zeroes such that the output has at least length 6.
x is the hex format specifier.

You can read all about it here.

timgeb
  • 76,762
  • 20
  • 123
  • 145