7

I made this mistake:

key, value = 'K', 999
msg = (
    f"key={key}, "
    "value={value}"  # needs to be prefixed with f as well
    )
# key=K, value={value}

and started wondering how Python handles complex cases of literal concatenation.

Let's assume one string is f-string (formatted string literal) and the other a plain string literal as in the example above. Does Python concatenate such two strings at compile time? And if yes what is the result?

bad_coder
  • 11,289
  • 20
  • 44
  • 72
VPfB
  • 14,927
  • 6
  • 41
  • 75

1 Answers1

7

From PEP 498:

Adjacent f-strings and regular strings are concatenated. Regular strings are concatenated at compile time, and f-strings are concatenated at run time.

Each f-string is entirely evaluated before being concatenated to adjacent f-strings.

Community
  • 1
  • 1
ely
  • 74,674
  • 34
  • 147
  • 228