Python将字符串拆分为多个字符串

2024-06-28 19:33:46 发布

您现在位置:Python中文网/ 问答频道 /正文

Possible Duplicate:
Split string into a list in Python

我有一根有很多部分弦的弦

>>> s = 'str1, str2, str3, str4'

现在我有一个如下的功能

>>> def f(*args):
        print(args)

我需要的是将字符串拆分成多个字符串,以便我的函数打印如下内容

>>> f(s)
('str1', 'str2', 'str3', 'str4')

有人知道我该怎么做吗?

  • 编辑: 我没有搜索将字符串拆分为字符串数组的函数。

这就是我要找的。

>>> s = s.split(', ')
>>> f(*s)
('str1', 'str2', 'str3', 'str4')

Tags: 函数字符串in功能stringargslistsplit
3条回答

有点谷歌会发现这个。。

string = 'the quick brown fox'
splitString = string.split()

...

['the','quick','brown','fox']

可以使用split()拆分字符串。语法如下。。。

stringtosplit.split('whattosplitat')

要在每个逗号和空格处拆分示例,请执行以下操作:

s = 'str1, str2, str3, str4'
s.split(', ')

尝试:

s = 'str1, str2, str3, str4'
print s.split(',')

相关问题 更多 >