+ 2
Splitting strings without using split function.
I want to make code to split a sentence by spaces For ex Have a nice day , results to ['Have','a,'nice','day']
10 Antworten
+ 4
You can use regex . Look at my example. Hope it helps you 😉
https://code.sololearn.com/c21CFfkWigqp/?ref=app
+ 4
https://code.sololearn.com/coJJd5SQuIr6/?ref=app
+ 1
import re
pattern = '\w+\s?'
mysentence = 'have a nice day'
print(re.findall(pattern, mysentence))
+ 1
You can write a while loop and increase an index variable i, until your_string[i] is ' '.
Then you append the string to that point to a list.
With two variables, you can steer that in a way that you can slice always the right part between two ' '.
+ 1
best I could do:-
mysentence = 'Have a nice day'
myword = ''
mylist = []
for x in mysentence + ' ': # didn't like to do this but... otherwise, last word not appended.
if x.isalpha():
myword += x
else:
mylist.append(myword)
myword = ''
print(mylist)
0
TheWh¡teCat rodwynnejones your answers are great but at school we are not allowed to use that as we are not taught(This is very bad though)
So can u split with just using the basics like loops lists etc.
0
HonFu I tried something like that
https://code.sololearn.com/csn66XHwrlMh/?ref=app
0
rodwynnejones can u correct the code i posted
0
HonFu can u make the code without using readymade functions except len.
Or can u correct the code I posted.
- 1
ok