Python字符串分割函数

Python学习之字符串分割

设计一个函数把一个含有空格的字符串分割成几个字符串


def splice1(temp):
    temp=temp.strip()
    while temp.find(' ')!=-1:
        temp1=temp[:temp.find(' ')]
        print temp1.strip()
        temp=temp[temp.find(' ')+1:].strip()
    print temp[temp.find(' ')+1:]


s2=raw_input('Plz input a string:')
splice1(s2)

例子:

Plz input a string:    i hiue  iuei  liui MIUI 9898&& *(((( LII  ELI LE  IE KJIjiJIJIE 179080 08098

ihiue
iuei
liui
MIUI
9898&&
*((((
LII
ELI
LE
IE

KJIjiJIJIE

179080

08098


增强版的字符串切割函数

src为要切割的字符串,sep为切割字符

def my_split(src,sep):
    src=src.strip()
    a=src.find(sep)
    w1=src[:a]
    print w1
    b=a
    n=len(sep)
    while b<=len(src) and b!=-1:
        while src[a:a+n]==sep:
            a=a+n
        b=src.find(sep,a)
        if b!=-1:
            print src[a:b]
        else:
            print src[a:b]
        a=b
s=raw_input('Plz input your string need to splitted:')
sp=raw_input('Plz input your splitting string:')
my_split(s,sp)

例子:

Plz input your string need to splitted:    jiji....sjeij...2....42.s.r..fs/.er.;s.plep.s;e,;l;slflep...s,eplps,e.pp..koio    
Plz input your splitting string:..
jiji
sjeij
.2
42.s.r
fs/.er.;s.plep.s;e,;l;slflep
.s,eplps,e.pp
koi

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值