python序列類型轉換內建函數

<code>string = "hello,world!"
list(string) #將字符串轉換為列表
Out[2]: ['h', 'e', 'l', 'l', 'o', ',', 'w', 'o', 'r', 'l', 'd', '!']

tuple(string) #將字符串轉換為元組
Out[3]: ('h', 'e', 'l', 'l', 'o', ',', 'w', 'o', 'r', 'l', 'd', '!')

len(string) #求字符串長度
Out[4]: 12

sorted(string) #對字符串進行排序
Out[5]: ['!', ',', 'd', 'e', 'h', 'l', 'l', 'l', 'o', 'o', 'r', 'w']

max(string) #求序列中的最大值
Out[6]: 'w'

min(string) #求序列中的最小值
Out[7]: '!'

sum((1,2,3)) #對序列進行求和
Out[8]: 6

sum([1,2,3]) #對序列進行求和
Out[9]: 6

list(reversed(string)) #逆序排序
Out[10]: ['!', 'd', 'l', 'r', 'o', 'w', ',', 'o', 'l', 'l', 'e', 'h']

list(enumerate((string))) #輸出元素與其對應的索引
Out[11]:
[(0, 'h'),
(1, 'e'),
(2, 'l'),
(3, 'l'),
(4, 'o'),
(5, ','),
(6, 'w'),
(7, 'o'),
(8, 'r'),
(9, 'l'),
(10, 'd'),
(11, '!')]/<code>




python序列類型轉換內建函數


分享到:


相關文章: