如何在 Python 中拆分字符串?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1838674/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 23:11:15  来源:igfitidea点击:

How to split a string in Python?

pythonstring

提问by Udders

I have read the documentation but don't fully understand how to do it.

我已阅读文档,但不完全了解如何操作。

I understand that I need to have some kind of identifier in the string so that the functions can find where to split the string (unless I can target the first space in the sentence?).

我知道我需要在字符串中有某种标识符,以便函数可以找到拆分字符串的位置(除非我可以定位句子中的第一个空格?)。

So for example how would I split: "Sico87 is an awful python developer"to "Sico87"and "is an awful Python developer"?

例如,我将如何拆分: "Sico87 is an awful python developer"to"Sico87""is an awful Python developer"

The strings are retrieved from a database (if this does matter).

从数据库中检索字符串(如果这很重要)。

回答by Stephan202

Use the splitmethod on strings:

split在字符串上使用该方法:

>>> "Sico87 is an awful python developer".split(' ', 1)
['Sico87', 'is an awful python developer']

How it works:

怎么运行的:

  1. Every string is an object. String objects have certain methods defined on them, such as splitin this case. You call them using obj.<methodname>(<arguments>).
  2. The first argument to splitis the character that separates the individual substrings. In this case that is a space, ' '.
  3. The second argument is the number of times the split should be performed. In your case that is 1. Leaving out this second argument applies the split as often as possible:

    >>> "Sico87 is an awful python developer".split(' ')
    ['Sico87', 'is', 'an', 'awful', 'python', 'developer']
    
  1. 每个字符串都是一个对象。字符串对象在其上定义了某些方法,例如split在这种情况下。您可以使用obj.<methodname>(<arguments>).
  2. 第一个参数split是分隔各个子字符串的字符。在这种情况下,这是一个空格,' '
  3. 第二个参数是应该执行拆分的次数。在你的情况下是1。省略第二个参数会尽可能多地应用拆分:

    >>> "Sico87 is an awful python developer".split(' ')
    ['Sico87', 'is', 'an', 'awful', 'python', 'developer']
    

Of course you can also store the substrings in separate variables instead of a list:

当然,您也可以将子字符串存储在单独的变量而不是列表中:

>>> a, b = "Sico87 is an awful python developer".split(' ', 1)
>>> a
'Sico87'
>>> b
'is an awful python developer'

But do note that this will cause trouble if certain inputs do not contain spaces:

但请注意,如果某些输入不包含空格,这将导致麻烦:

>>> a, b = "string_without_spaces".split(' ', 1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: need more than 1 value to unpack

回答by Will

Use partition(' ')which always returns three items in the tuple - the first bit up until the separator, the separator, and then the bits after. Slots in the tuple that have are not applicable are still there, just set to be empty strings.

使用partition(' ')which 总是返回元组中的三个项目 - 第一位直到分隔符,分隔符,然后是后面的位。元组中不适用的插槽仍然存在,只是设置为空字符串。

Examples: "Sico87 is an awful python developer".partition(' ')returns ["Sico87"," ","is an awful python developer"]

示例: "Sico87 is an awful python developer".partition(' ')退货["Sico87"," ","is an awful python developer"]

"Sico87 is an awful python developer".partition(' ')[0]returns "Sico87"

"Sico87 is an awful python developer".partition(' ')[0]回报 "Sico87"

An alternative, trickierway is to use split(' ',1)which works similiarly but returns a variablenumber of items. It will return a tuple of one or two items, the first item being the first word up until the delimiter and the second being the rest of the string (if there is any).

另一种更棘手的方法是使用split(' ',1)which 工作方式类似但返回可变数量的项目。它将返回一个或两个项目的元组,第一个项目是第一个单词,直到分隔符,第二个项目是字符串的其余部分(如果有的话)。