How to separate a string in these words in python | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How to separate a string in these words in python

Like my string is " Welcome my brother" and to output: "Welcome" as the first word "my" as the second and "brother" as the last..

16th Sep 2022, 3:32 PM
Mazanosh✨✨
Mazanosh✨✨ - avatar
8 Answers
+ 5
Mazanosh✨✨ , to get individual words from a string, we can split the string by using the .split() method. this generates a list of strings that can then be used for output. to do so, we can use a for loop with a print() statement inside. using this input: " Welcome my brother" will get: ["Welcome", "my", "brother"]
16th Sep 2022, 6:37 PM
Lothar
Lothar - avatar
+ 3
Hi! what are your thoughts on this?
16th Sep 2022, 3:37 PM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
+ 1
I think to separate a string if it is a sentence in these words E.g. input: " hey Friend, Come here" Output: hey Friend Come here
16th Sep 2022, 3:56 PM
Mazanosh✨✨
Mazanosh✨✨ - avatar
+ 1
how will you determine the end of a word?
16th Sep 2022, 4:16 PM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
+ 1
Right! create a variable, place a space there, and compare the phrase to the variable. in a cycle
16th Sep 2022, 4:29 PM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
0
By the Space, if there is a Space between to letter we can Say that it the end of the word
16th Sep 2022, 4:21 PM
Mazanosh✨✨
Mazanosh✨✨ - avatar
0
Here goes the code.. text = "This is my code." list = text.split() for i in list: print(i) Alternatively, you can use: text = "This\nis\nmy\ncode" print(text) #\n is a newline function
17th Sep 2022, 1:12 PM
Diksha Sharma
Diksha Sharma - avatar
0
use \n (backslash N) to make a newline
18th Sep 2022, 1:01 PM
MrBeast
MrBeast - avatar