python—内置函数-字符串,eval,isinstance
生活随笔
收集整理的這篇文章主要介紹了
python—内置函数-字符串,eval,isinstance
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
eval()
功能:將字符串str當成有效的表達式來求值并返回計算結果。
語法:?eval(source[, globals[, locals]]) -> value
參數:
source:一個Python表達式或函數compile()返回的代碼對象
globals:可選。必須是dictionary
locals:可選。任意map對象
實例展示:
可以把list,tuple,dict和string相互轉化。################################################# 字符串轉換成列表 >>>a = "[[1,2], [3,4], [5,6], [7,8], [9,0]]" >>>type(a) <type 'str'> >>> b = eval(a) >>> print b [[1, 2], [3, 4], [5, 6], [7, 8], [9, 0]] >>> type(b) <type 'list'> ################################################# 字符串轉換成字典 >>> a = "{1: 'a', 2: 'b'}" >>> type(a) <type 'str'> >>> b = eval(a) >>> print b {1: 'a', 2: 'b'} >>> type(b) <type 'dict'> ################################################# 字符串轉換成元組 >>> a = "([1,2], [3,4], [5,6], [7,8], (9,0))" >>> type(a) <type 'str'> >>> b = eval(a) >>> print b ([1, 2], [3, 4], [5, 6], [7, 8], (9, 0)) >>> type(b) <type 'tuple'>isinstance() def isAString(obj):return isinstance(obj, str)if isAString('1'):print('是字符串')
?
轉載于:https://www.cnblogs.com/xiaobai-yemao/p/9153314.html
總結
以上是生活随笔為你收集整理的python—内置函数-字符串,eval,isinstance的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 解决问题SyntaxError: Une
- 下一篇: win主机ping不通linux的IP