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\']
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(""))
Starting with a list of strings, if you would like to split these strings there are a couple ways to do so depending on what your desired output is.
Case 1: One list of strings (old_list
) split into one new list of strings (new_list
).
For example ['This is a Sentence', 'Also a sentence']
-> ['This', 'is', 'a', 'Sentence', 'Also', 'a', 'sentence']
.
Steps:
for sentence in old_list:
word
). for ch in sentence:
word
is not empty and add it to the new list, otherwise add the character to word
.word
to the list after looping through all the characters.The final code:
new_list = []
for sentence in old_list:
word = ''
for ch in sentence:
if ch == ' ' and word != '':
new_list.append(word)
word = ''
else:
word += ch
if word != '':
new_list.append(word)
This is equivalent to
new_list = []
for sentence in old_list:
new_list.extend(sentence.split(' '))
or even simpler
new_list = ' '.join(old_list).split(' ')
Case 2: One list of strings (old_list
) split into a new list of lists of strings (new_list
).
For example ['This is a Sentence', 'Also a sentence']
-> [['This', 'is', 'a', 'Sentence'], ['Also', 'a', 'sentence']]
.
Steps:
for sentence in old_list:
word
) and a new list to keep track of the words in this string (sentence_list
). for ch in sentence:
word
is not empty and add it to sentence_list
, otherwise add the character to word
.word
to sentence_list
after looping through all the characters.Append
(not extend
) sentence_list
to the new list and move onto the next string.The final code:
new_list = []
for sentence in old_list:
sentence_list = []
word = ''
for ch in sentence:
if ch == ' ' and word != '':
sentence_list.append(word)
word = ''
else:
word += ch
if word != '':
sentence_list.append(word)
new_list.append(sentence_list)
This is equivalent to
new_list = []
for sentence in old_list:
new_list.append(sentence.split(' '))
or using list comprehensions
new_list = [sentence.split(' ') for sentence in old_list]
string1 = 'bella ciao amigos'
split_list = []
tmp = ''
for s in string1:
if s == ' ':
split_list.append(tmp)
tmp = ''
else:
tmp += s
if tmp:
split_list.append(tmp)
print(split_list)
Output: ------> ['bella', 'ciao', 'amigos']
reverse_list = split_list[::-1]
print(reverse_list)
Output: ------> ['amigos', 'ciao', 'bella']
This is simple code to split a char value from a string value; i.e
INPUT : UDDDUDUDU
s = [str(i) for i in input().strip()]
print(s)
OUTPUT: ['U','D','D','D','U','D','U','D','U']
You can use regular expressions if you want:
>>> import re
>>> s = 'This is a Sentence'
>>> re.findall(r'\S+', s)
['This', 'is', 'a', 'Sentence']
The \S
represents any character that isn't whitespace, and the +
says to find one or more of those characters in a row. re.findall
will create a list
of all strings that match that pattern.
But, really, s.split()
is the best way to do it.
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)