要將字符串轉換為列表和元組,可以使用Python的內置函數list()
和tuple()
。以下是示例代碼:
# 將字符串轉換為列表
my_string = "hello"
my_list = list(my_string)
print(my_list) # 輸出: ['h', 'e', 'l', 'l', 'o']
# 將字符串轉換為元組
my_tuple = tuple(my_string)
print(my_tuple) # 輸出: ('h', 'e', 'l', 'l', 'o')