Python_API_String Services_string.split

本文详细介绍了Python中字符串split方法的使用方式,包括如何通过指定不同的参数实现字符串的分割操作。文章提供了多个示例,展示了当参数为空或指定特定字符串时,split方法的行为表现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

API文档:

string.split(s[,sep[, maxsplit]])

Return a list of the words of the string s. If the optional second argumentsep is absent or None, the words are separated by arbitrary strings of whitespace characters (space, tab, newline, return, formfeed). If the second argumentsep is present and not None, it specifies a string to be used as the word separator. The returned list will then have one more item than the number of non-overlapping occurrences of the separator in the string. The optional third argument maxsplit defaults to 0. If it is nonzero, at mostmaxsplit number of splits occur, and the remainder of the string is returned as the final element of the list (thus, the list will have at mostmaxsplit+1 elements).

The behavior of split on an empty string depends on the value ofsep. If sep is not specified, or specified as None, the result will be an empty list. If sep is specified as any string, the result will be a list containing one element which is an empty string.

翻译文档:
参数:
                 s:目标字符串
    sep:分割字符串
                 maxsplit:
描述:
                  该方法返回一个s字符串的分割后的list。
     如果可选项的第二个参数sep不填写,则目标字符串根据空白字符(space, tab, newline, return, formfeed)进行分割.
                  如果第二个参数填写,则使用该字符串进行字符串分割。
     如果第三个参数填写,则匹配次数
例子:#! /usr/bin/env python#coding=utf-8
import string
str="a <nd abc <nd nd > "

print string.split(str)print string.split(str,'a')print string.split(str,' ',1)print string.split(str,' ',2)print string.split(str,'sdf')输出:['a', '<nd', 'abc', '<nd', 'nd', '>']['', ' <nd ', 'bc <nd nd > ']['a', '<nd abc <nd nd > ']['a', '<nd', 'abc <nd nd > ']['a <nd abc <nd nd > ']



 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值