Splitting strings in Python without split()

后端 未结 8 768
醉话见心
醉话见心 2021-01-17 03:59

What are other ways to split a string without using the split() method? For example, how could [\'This is a Sentence\'] be split into [\'This\', \'is\', \'a\', \'Sentence\']

8条回答
  •  独厮守ぢ
    2021-01-17 04:24

    def mysplit(strng):
        strng = strng.lstrip() 
        strng = strng.rstrip()
        lst=[]
        temp=''
        for i in strng:
            if i == ' ':
                lst.append(temp)
                temp = ''
            else:
                temp += i
        if temp:
            lst.append(temp)
        return lst
         
    print(mysplit("Hello World"))
    print(mysplit("   "))
    print(mysplit(" abc "))
    print(mysplit(""))
    

提交回复
热议问题