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..
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"]
+ 3
Hi! what are your thoughts on this?
+ 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
+ 1
how will you determine the end of a word?
+ 1
Right! create a variable, place a space there, and compare the phrase to the variable. in a cycle
0
By the Space, if there is a Space between to letter we can Say that it the end of the word
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
0
use \n (backslash N) to make a newline