python-small-examples

@author jackzhenguo
@desc 
@date 2019/2/15

15 任意对象转为字符串  

                                                              

In [1]: str(100)                                                                 
Out[1]: '100'

In [2]: str([3,2,10])                                                           
Out[2]: '[3, 2, 10]'

In [3]: str({'a':1, 'b':10})                                                    
Out[3]: "{'a': 1, 'b': 10}"

In [11]: from collections import defaultdict                                    
In [12]: dd = defaultdict(int)                                                  

In [14]: for i in [1,3,2,2,3,3]: 
    ...:     dd[i] += 1 
    ...:                                                                        

In [15]: dd                                                                     
Out[15]: defaultdict(int, {1: 1, 3: 3, 2: 2})

In [16]: str(dd)                                                                
Out[16]: "defaultdict(<class 'int'>, {1: 1, 3: 3, 2: 2})"

[上一个例子](/python-small-examples/md/14.html) [下一个例子](/python-small-examples/md/16.html)