给定一个字符串:
它是由逗号分隔的几个值的序列:
mStr = '192.168.1.1,192.168.1.2,192.168.1.3'
如何将字符串转换为列表?
mStr = ['192.168.1.1', '192.168.1.2', '192.168.1.3']
使用str.split方法:
>>> mStr = '192.168.1.1,192.168.1.2,192.168.1.3' >>> mStr.split(",") ['192.168.1.1', '192.168.1.2', '192.168.1.3']
将字符串转成元组:
>>> mlist = mStr.split(",") >>> tuple(mlist) ('192.168.1.1', '192.168.1.2', '192.168.1.3') >>>