Print each word of the sentence in a new line python
I want to print each word in Show I've tried using a
The result should be like this:
Mad Physicist 101k24 gold badges164 silver badges247 bronze badges asked Jan 10, 2019 at 5:59
0 If you absolutely need to use index slicing:
Output:
But why not use
Output:
answered Jan 10, 2019 at 6:21
iz_iz_ 15.1k2 gold badges25 silver badges40 bronze badges I think I know what this problem is for (edx class..as I ran into the same thing). This solution worked for me using the pieces they're encouraging the students to use at this point in the course:
answered Feb 8, 2019 at 20:41
lee_flee_f 111 bronze badge Posting another way if a student needs an example and must do index slicing.
answered Sep 22 at 19:40
The obvious solution would be to use
A better way might be to keep track of an index for the current space and keep looking for the next one. This is probably similar to what you had in mind, but your loop doesn't update and variables:
A shortcut that probably won't be acceptable either, but produces the desired output:
answered Jan 10, 2019 at 6:31
Mad PhysicistMad Physicist 101k24 gold badges164 silver badges247 bronze badges Not sure why anyone would want to do this rather than just use
answered Jan 10, 2019 at 6:53
benvcbenvc 13.9k4 gold badges31 silver badges51 bronze badges How do you print each word of the sentence in a new line?First, we will take a character array and take a sentence as an input and store it in the array. Then we will traverse the array using a while loop until the end of it, i.e., until we find the '\0' character. Then if we find a ' ' (blank space character) we will print a new-line character (\n).
How do you print a new line of sentence in Python?The new line character in Python is \n . It is used to indicate the end of a line of text. You can print strings without adding a new line with end =
How do you print each word in a string in Python?Approach: Split the string using split() function. Iterate in the words of a string using for loop. Calculate the length of the word using len() function. If the length is even, then print the word.
How do you print on different lines in Python?"\n" can be used for a new line character, or if you are printing the same thing multiple times then a for loop should be used.
|