0

I can use fstring with a dictionary:

mydict = {'foo': 'py', 'bar': 'thon'}
print(f"I am using {mydict['foo']}{mydict['bar']}")
> I am using python

I can also pad strings:

f"{'Foo': <16} Bar"
>'Foo              Bar'

I want to be able to pad a string that's coming from my dictionary inside my fstring.

I have tried:

f"{{mydict['foo']}: <16} {mydict['bar']}"

The error here is:

File "", line 1
SyntaxError: f-string: single '}' is not allowed

Is this possible or do I have to add the padded string to my dictionary first before using?

Tim Edwards
  • 1,031
  • 1
  • 13
  • 34

1 Answers1

1

the inner {} is superfluous:

mydict = {'foo': 'py', 'bar': 'thon'}
f"{mydict['foo']: <16} {mydict['bar']}"

works.

vaizki
  • 1,678
  • 1
  • 9
  • 12