如何将字符串过滤到Python上的列表

How to filter a string to a list on Python

本问题已经有最佳答案,请猛点这里访问。

我想过滤这样的字符串:"hello%world"-->list=["hello","world"]有内置函数吗?.


您可以使用str.split

1
2
3
>>> strs='Hello%World'
>>> strs.split("%")
['Hello', 'World']

帮助(str.split):

S.split(sep=None, maxsplit=-1) -> list of strings

Return a list of the words in S, using sep as the delimiter string.
If maxsplit is given, at most maxsplit splits are done. If sep is not
specified or is None, any whitespace string is a separator and empty
strings are removed from the result.


简单拆分

1
2
str ='Hello%World'
print str.split("%")

给你

1
2
>>>
['Hello', 'World']