元祖跟列表之间的转化:
tu = (1, 2, 3) l = list(tu) print(l, type(l)) li = [4, 5, 6] print(tuple(li),type(tuple(li))) 输出: [1, 2, 3] <class ‘list‘> (4, 5, 6) <class ‘tuple‘>
字符串转化成列表:
split:
s = "ab_c" lst = s.split("_") print(lst) 输出: [‘ab‘, ‘c‘]
或者:
s = "ab_c" print(list(s)) 输出: [‘a‘, ‘b‘, ‘_‘, ‘c‘]
列表转化成字符串:
join
lst = ["a", "b", "c", "d"] s = "*".join(lst) print(s) 输出: a*b*c*d
或者:
lst = ["a", "b", "c", "d"] s = str(lst) print(s,type(s)) 输出:[‘a‘, ‘b‘, ‘c‘, ‘d‘] <class ‘str‘>
字符串转化成元祖:
s = "ab_c" t = tuple(s) print(t, type(t)) 输出: (‘a‘, ‘b‘, ‘_‘, ‘c‘) <class ‘tuple‘>
1, int ------> str: str(int)
时间: 2024-10-21 09:44:21