Python 学习

来源:互联网 发布:居外网软件下载 编辑:程序博客网 时间:2024/06/16 14:28


1.  str  list  互转

def str_list():    s = "123abc"    list_tmp = list(s)    print (list_tmp)    print (''.join(list_tmp))    print ('\n')        s = "123,abc,456"    list_tmp = s.split(",")    print (list_tmp)    print (','.join(list_tmp))

结果:

['1', '2', '3', 'a', 'b', 'c']123abc['123', 'abc', '456']123,abc,456


2. eval 使用

    s = "['abc', '123', 'eed']"    value = eval(s)    print (type(value))    print (value)


结果:

<type 'list'>['abc', '123', 'eed']


字符串转字典

    d = "{'abc':123, 'cbd':234}"    value = eval(d)    print (type(value))    print (value)

结果:

<type 'dict'>{'abc': 123, 'cbd': 234}

字符串转元组

    t = "(2, 'qwer', ['2wqe','abc'], 34)"    value = eval(t)    print (type(value))    print (value)

结果:

<type 'tuple'>(2, 'qwer', ['2wqe', 'abc'], 34)


3. 字符串截取

(1)  截掉后面的字符串

    s = '00E04C800628-chrome'    print (s.rstrip('-chrome'))




原创粉丝点击