Problems formatting a string - Python 2.7.3 -
i'm coming across little weirdness str.format() method , wondered if there might workaround.
here basic example of problem facing:
'something {first.alpha} {last}'.format(**{'first.alpha':'then', 'last':'else'})
i expect return:
"something else"
but instead error:
keyerror: 'first'
i'm aware there other approaches formatting string, until approach seemed perfect fit needed.
the following example works perfectly, important 'first.alpha' key exists.
'something {first} {last}'.format(**{'first':'then', 'last':'else'})
is there way might still able use str.format() method , contain fullstops within key?
the way use named arguments in format
this
>>> 'something {first_alpha} {last}'.format(first_alpha = 'then', last = 'else') 'something else'
i wouldn't use first.alpha
because thinks first
has attribute alpha
.
Comments
Post a Comment