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

编程入门 行业动态 更新时间:2024-10-23 22:35:56
本文介绍了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将字符串拆分为多个字符串

本文发布于:2023-08-07 13:53:37,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1319411.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   多个   Python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!