Python将字符串拆分为多个字符串 [英] Python split string into multiple string

查看:1963
本文介绍了Python将字符串拆分为多个字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
将字符串分割成Python列表

Possible Duplicate:
Split string into a list in Python

我的字符串中包含很多部分字符串

I have a string with a lot of part-strings

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

现在我有一个类似下面的功能

now I have a function like following

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

我需要的是将我的字符串分成多个字符串,以便我的函数打印出这样的内容

what I need, is to split my string into multiple strings, so that my function prints something like this

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

有人对我有想法吗?

  • 我没有搜索将字符串拆分为字符串数组的函数.
  • edit: I did not searched a function to split a string into an array of Strings.

这是我搜索的内容.

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

推荐答案

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

You can split a string by using split(). The syntax is as follows...

stringtosplit.split('whattosplitat')

要在每个逗号和空格处分隔示例,应为:

To split your example at every comma and space, it would be:

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

这篇关于Python将字符串拆分为多个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆