Quantcast
Channel: CodeSection,代码区,Python开发技术文章_教程 - CodeSec
Viewing all articles
Browse latest Browse all 9596

Python 字典与字符串的互转实例

$
0
0

字典转换为字符串

if __name__ == '__main__':
a = {'a' : 1, 'b' : 2, 'c' : 3}
b = str(a)
print(type(b))

输出结果为:

<class 'str'>

---------------------------------------------------------------

字符串转换为字典

if __name__ == '__main__':
a = "{'a' : 1, 'b' : 2, 'c' : 3}"
b = eval(a)
print(type(b))

输出结果为:

<class 'dict'>

以上这篇python 字典与字符串的互转实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。


Viewing all articles
Browse latest Browse all 9596

Trending Articles