Python分割,排除字符串的一部分

这是我得到的字符串 Enter 3 random strings, separated by commas: The,Dave,Make

我希望该列表仅包含以下内容:["The", "Dave", "Make"]

我尝试过使用 split,但它导致结果错误

strings = input("Enter 3 random strings, separated by commas:")
strings = strings.split()


MMMHUHU
浏览 49回答 4
4回答

RISEBY

使用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']

弑天下

在冒号处拆分字符串,删除多余的空格,然后在逗号处拆分。string = 'Enter 3 random strings, separated by commas: The,Dave,Make' result = string.split(':')[1].strip().split(',')

慕森卡

如果字符串是从控制台获取的,那么它的第一部分应该是输入提示:strings = input("Enter 3 random strings, separated by commas:")strings = strings.strip().split(',')代码可以缩短为一行:strings = input("Enter 3 random strings, separated by commas:").strip().split(',')

梦里花落0921

尝试这个:string = 'Enter 3 random strings, separated by commas: The,Dave,Make'lst = string.split(':')[-1].strip().split(',')输出:>>> lst['The', 'Dave', 'Make']
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python