1

I'm trying to format a string with incomplete kwargs as below:

input is like "{key1} {key2} {key3}"
output will be like "value1 value2 {key3}"

I try below,

>>> "{key1} {key2} {key3}".format(key1='value1', key2='value2')

but got following error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 'key3'

Any idea on what's the best way to do it?

zhutoulala
  • 4,792
  • 2
  • 21
  • 34

1 Answers1

3

Two ways to fix this,

  1. Double escape the pattern

    >>> "{key1} {key2} {{key3}}".format(key1='value1', key2='value2')
    
  2. Or pass {key3} as the value

    >>> "{key1} {key2} {key3}".format(key1='value1',key2='value2',key3='{key3}')
    
thefourtheye
  • 233,700
  • 52
  • 457
  • 497