2014-10-18 16 views
-2
In [4]: {'a': "hello"}['a'] 
Out[4]: 'hello' 

我所要的輸出改爲:如何在沒有引號的情況下打印一個dicionary的字符串值?

Out[4]: hello 

什麼是最優雅的方式來實現這一目標? 。

我也使用再版()rjust()稍後打印的詞典列表成整齊的格式化的表格,我注意到再版功能還增加了引號是煩人:

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

產量:

'hello' 

時,我想:

 hello 
+1

'repr'打印值的表示形式,而不是值本身,在字符串的情況下包含引號。 – davidism 2014-10-18 22:37:59

+2

'print({'a':「hello」} ['a'])',如果你不想引號,爲什麼要使用repr? – 2014-10-18 22:38:25

+0

http://stackoverflow.com/questions/7784148/understanding-repr-function-in-python – 2014-10-18 22:51:51

回答

0

剛落repr

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

謝謝!我不知道我爲什麼使用repr。我只是從一個有它的例子中複製它,但現在我意識到它是不必要的。 – cas5nq 2014-10-18 23:05:35

相關問題