Print each word of the sentence in a new line python

I want to print each word in word = "They stumble who run fast" on a new line using index slicing.

I've tried using a while loop, like printing words after each space

word = "They stumble who run fast"
space = word.count[' ']
start = 0
while space != -1:
   print[word[start:space]]

The result should be like this:

They
stumble
who
run
fast

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:

word = "They stumble who run fast"

indexes = [i for i, char in enumerate[word] if char == ' ']

for i1, i2 in zip[[None] + indexes, indexes + [None]]:
    print[word[i1:i2].strip[]]

Output:

They
stumble
who
run
fast

But why not use .split[]?

word = "They stumble who run fast"
print[*word.split[], sep='\n']

Output:

They
stumble
who
run
fast

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:

quote = "they stumble who run fast"
start = 0
space_index = quote.find[" "]
while space_index != -1:
    print [quote[start:space_index]]
    start = space_index+1
    space_index = quote.find[" ", space_index+1]
else:
    print [quote[start::1]]

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.

    check = 0 # Here we start slicing at zero
    
    split = 0
    
    for x in word:
        if x == ' ':  # Will check for spaces
            print[word[check:split]]
            check = split + 1  # check will inherit splits value plus one when space found
        split = split + 1  # spit will increase each time no space is found
    print[word[check:split]]  # needed to print final word

answered Sep 22 at 19:40

The obvious solution would be to use str.split, but that would violate your desire for slicing:

for w in word.split[]:
    print[w]

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:

start = 0
try:
    while True:
        end = word.index[' ', start]
        print[word[start:end]]
        start = end + 1
except ValueError:
    print[word[start:]]

A shortcut that probably won't be acceptable either, but produces the desired output:

print[word.replace[' ', '\n']]

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 str.split[], but here is another [fairly ugly] way along the lines of your initial stab at it.

word = "They stumble who run fast"
while ' ' in word:
    i = word.index[' ']
    print[word[:i]]
    word = word[i+1:]
print[word]

# OUTPUT
# They
# stumble
# who
# run
# fast

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 = , which is the character that will be used to separate the lines.

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.

Chủ Đề