+ 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']

28th Sep 2019, 4:00 PM
Yash
Yash - avatar
10 Respuestas
+ 4
You can use regex . Look at my example. Hope it helps you 😉 https://code.sololearn.com/c21CFfkWigqp/?ref=app
28th Sep 2019, 4:23 PM
TheWh¡teCat 🇧🇬
TheWh¡teCat 🇧🇬 - avatar
+ 4
https://code.sololearn.com/coJJd5SQuIr6/?ref=app
4th Oct 2019, 10:20 AM
HonFu
HonFu - avatar
+ 1
import re pattern = '\w+\s?' mysentence = 'have a nice day' print(re.findall(pattern, mysentence))
28th Sep 2019, 4:29 PM
rodwynnejones
rodwynnejones - avatar
+ 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 ' '.
28th Sep 2019, 4:34 PM
HonFu
HonFu - avatar
+ 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)
28th Sep 2019, 6:31 PM
rodwynnejones
rodwynnejones - avatar
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.
28th Sep 2019, 4:31 PM
Yash
Yash - avatar
28th Sep 2019, 4:41 PM
Yash
Yash - avatar
0
rodwynnejones can u correct the code i posted
29th Sep 2019, 2:06 AM
Yash
Yash - avatar
0
HonFu can u make the code without using readymade functions except len. Or can u correct the code I posted.
4th Oct 2019, 9:39 AM
Yash
Yash - avatar
- 1
ok
5th Oct 2019, 3:42 PM
Ļmhbœłê Řæjãwi
Ļmhbœłê Řæjãwi - avatar