/* 一: str -> list 二: list -> str 1 list元素全为str 2 list元素不全为str */
一: str -> list
if __name__ == '__main__': str1 = "12345" print(list(str1)) # ['1', '2', '3', '4', '5'] str1 = "123 abc jik" print(str1.split()) # ['123', 'abc', 'jik'] str1 = "www.baidu.com" print(str1.split(".")) # ['www', 'baidu', 'com']
二: list -> str
1 list元素全为str
if __name__ == '__main__': k = ["11", "22", "-3"] print("k:", "".join(k)) # k: 1122-3
2 list元素不全为str
if __name__ == '__main__': # 报错 k = ["11", "22", -3] print("k:", "".join(k)) # TypeError: sequence item 2: expected str instance, int found # 方法一 k = ["11", "22", -3] s = "" for i in k: s += (str(i)) print("k:", s) # k: 1122-3 # 方法一 k = [11, "22", -3] print("k:", "".join(str(k))) # k: [11, '22', -3]
标签:类型转换,__,string,22,str1,list,str,print From: https://www.cnblogs.com/huafan/p/17362577.html