2021年1月5日星期二

How do I use str.format() on a dictionary type? [closed]

How do I use str.format() on a dictionary type?

For example:

>>> a = 'zxv'  >>>  >>> dic = {'a' : 'val = abc', 'b' : 'val = {0}', 'c' : 'val = {0}'}.format(a)  Traceback (most recent call last):    File "<stdin>", line 1, in <module>  AttributeError: 'dict' object has no attribute 'format'  

< This gives an error.

>>> dic = {'a' : 'val = abc', 'b' : 'val = {0}'.format(a), 'c' : '{0}'}  >>> dic  {'a': 'val = abc', 'c': '{0}', 'b': 'val = zxv'}  

< This change makes it viable, but it causes omissions.

Is this a format not supported? Help plz.

I wanted to input multiple values at once using format(). I expected usability similar to the following:

>>> a = "hi"  >>> text = ('{0} {0}').format(a)  >>> text  'hi hi'  

And I'm still using python 2.7.

https://stackoverflow.com/questions/65575860/how-do-i-use-str-format-on-a-dictionary-type January 05, 2021 at 05:03PM

没有评论:

发表评论