Splitting strings in Python without split()

后端 未结 8 794
醉话见心
醉话见心 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:36

    sentence = 'This is a sentence'
    split_value = []
    tmp = ''
    for c in sentence:
        if c == ' ':
            split_value.append(tmp)
            tmp = ''
        else:
            tmp += c
    if tmp:
        split_value.append(tmp)
    

提交回复
热议问题