【问题标题】:How to print a string value of a dicionary without the quotes? [duplicate]如何打印不带引号的字典的字符串值? [复制]
【发布时间】:2014-10-18 22:35:36
【问题描述】:
In [4]: {'a': "hello"}['a']
Out[4]: 'hello'

我希望输出改为:

Out[4]: hello

实现这一目标最优雅的方法是什么?

我稍后也使用 repr().rjust() 将字典列表打印到格式整齐的表格中,我注意到 repr 函数还添加了令人讨厌的引号:

def print_dictionary(d): print repr(d['a']).rjust(10)
print_dictionary({'a':'hello'})

产量:

   'hello'

当我想要时:

     hello

【问题讨论】:

标签: python dictionary


【解决方案1】:

只需删除repr

def print_dictionary(d): print (d['a']).rjust(10)

【讨论】:

  • 谢谢!我不知道我为什么使用repr。我只是从一个拥有它的示例中复制它,但现在我意识到它是不必要的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-13
  • 2017-02-25
  • 2021-11-16
  • 1970-01-01
相关资源
最近更新 更多