4 回答

TA貢獻1856條經驗 獲得超5個贊
使用string.split()
方法strip()
:
In [2680]: s = "Enter 3 random strings, separated by commas: The,Dave,Make"
In [2686]: s.split(':')[-1].strip().split(',')
Out[2686]: ['The', 'Dave', 'Make']

TA貢獻1818條經驗 獲得超8個贊
在冒號處拆分字符串,刪除多余的空格,然后在逗號處拆分。
string = 'Enter 3 random strings, separated by commas: The,Dave,Make' result = string.split(':')[1].strip().split(',')

TA貢獻1806條經驗 獲得超8個贊
如果字符串是從控制臺獲取的,那么它的第一部分應該是輸入提示:
strings = input("Enter 3 random strings, separated by commas:")
strings = strings.strip().split(',')
代碼可以縮短為一行:
strings = input("Enter 3 random strings, separated by commas:").strip().split(',')

TA貢獻1772條經驗 獲得超6個贊
嘗試這個:
string = 'Enter 3 random strings, separated by commas: The,Dave,Make'
lst = string.split(':')[-1].strip().split(',')
輸出:
>>> lst
['The', 'Dave', 'Make']
添加回答
舉報