我想创建自己的函数,该函数用\n
(新行)分割字符串。我不想使用任何内置函数。该怎么办?
答案 0 :(得分:0)
尝试一下:
def split_string(string, delimiter):
output = []
while delimiter in string:
index = string.index(delimiter)
output.append(string[0:index])
string = string[index+1:]
output.append(string)
return output
str = """This is
a string
of words
delimited
by slash n
ok"""
split_string(str, "\n")
op:
['This is ', 'a string', 'of words', 'delimited', 'by slash n', 'ok']