在没有 split() 的情况下在 Python 中拆分字符串 [英] Splitting strings in Python without split()

查看:44
本文介绍了在没有 split() 的情况下在 Python 中拆分字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

还有哪些不使用 split() 方法来拆分字符串的方法?例如,如何在不使用 split() 方法的情况下将 ['This is a Sentence'] 拆分为 ['This', 'is', 'a', 'Sentence'] ?

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'] without the use of the split() method?

推荐答案

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)

这篇关于在没有 split() 的情况下在 Python 中拆分字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆