Python Forum

Full Version: slicing and counting words in a string
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Hello,
I'm working on an assignment that prompts the user for a string of words. It counts how many words and counts how many letters in each word.
For example, "hello night sky"
Output should look like:
len = 5 hello
len = 5 night
len = 3 sky
There are 3 words.

I've got the word count figured out and the total character count, but I'm having a hard time figuring out how to get it to slice the different words. I know how to do a slice if I know the index numbers, but since it's user input, how do I tell it where to stop?

**I'm not allowed to use .split() for this assignment.

Here's what I've got so far:

words = input('Enter a series of words separated by spaces: ')

print('The length of the input string is ',len(words))

wordCount = 1
charCount = 0

while charCount < len(words):
    x = words[charCount]
    print('len = ', charCount)
    
    print(x)
    charCount = charCount + 1
    

    if x == ' ':
        wordCount = wordCount + 1
        
    
print('There are ', wordCount, 'words')
Any hints or suggestions would be much appreciated. Thank you!

Rudy
If I were you, I'd implement my own split function that fulfills this need. Inside of that, you'd probably keep a current_start index which is initially 0, and whenever you hit whitespace you can reset it.
I think you should introduce a special flag, that will indicate beginning of a word. I began to write the code, but since this is an assignment, I pasted it partially:

import string

words = input('Enter a series of words separated by spaces: ')
 
print('The length of the input string is ',len(words))


current_word = ''
container = []
for j in words:
    word_beginning_flag = j in string.ascii_letters # you can define string.ascii_letters manually instead

    if word_beginning_flag:
        current_word += j

    if j not in string.ascii_letters:
        word_beginning_flag = False

# ....................
I forgot to mention this has to be a while loop. In setting a start index and then restarting whenever it hits white space - I've got it set to go back to 0, but I can't figure out how to get it to continue on to the next word.
Ok, the only thing you need is to rewrite the snippet below using while loop:
import string

words = input('Enter a series of words separated by spaces: ')
 
print('The length of the input string is ',len(words))


current_word = ''
container = []
for j in words:
    word_beginning_flag = j in string.ascii_letters # you can define string.ascii_letters manually instead

    if word_beginning_flag:
        current_word += j

    if j not in string.ascii_letters:
        word_beginning_flag = False
        if current_word:
            container.append((current_word, len(current_word)))
            current_word = ''
else:
    if words[-1] in string.ascii_letters:
        container.append((current_word, len(current_word)))

print("Result is ready: ", container)
Have you considered using reg ex ? If its an option. Maybe it can return the words then you just need to get the length of each word...

if you need to use a while loop through all chars...
loop all chars, for each char check if it's a word break (eg space or other chars)
if not a word break, then add the char to the current word
if it is a word break add the current word to a list (or dictionary if you are counting words individually) and reset the current word to ''
at the end you should have a list/dictionary of words (which you can loop to get the sizes of each word) and either length of list or total the occurances to give number of words.
Do you just need to print the word and length or are you going to use the list again after?
Time to time when I read these assignments dark side of me takes over. Today is one of those days, so I present 'Monty'-liner which delivers required output (though without while, so it's not qualifying as valid solution):

>>> from itertools import groupby
>>> s = 'hello night sky'
>>> print(*[f'len = {len(word)} {word}' for word in [''.join(group) for key, group in groupby(s, lambda x: x == ' ') if key == False]], f'There are {s.count(" ") + 1} words', sep='\n')
len = 5 hello
len = 5 night
len = 3 sky
There are 3 words
I have 3 different solutions. From very naive to pythonic.

def get_wordlen_count_stupid(text):
    text_len = len(text)
    words = []
    word_lengths = []
    word_count = 0
    pos = 0
    current = ''
    while pos < text_len:
        if text[pos] in string.whitespace and current:
            words.append(current)
            word_lengths.append(len(current))
            word_count += 1
            current = ''
        else:
            current += text[pos]
        pos += 1
    if current:
        words.append(current)
        word_lengths.append(len(current))
        word_count += 1
    return words, word_lengths, word_count
def get_wordlen_count(text):
    words = []
    word_lengths = []
    word_count = 0
    current = ''
    for char in text:
        if char in string.whitespace and current:
            words.append(current)
            word_lengths.append(len(current))
            word_count += 1
            current = ''
        else:
            current += char
    if current:
        words.append(current)
        word_lengths.append(len(current))
        word_count += 1
    return words, word_lengths, word_count
def simple_wordcount(text):
    words = text.split()
    word_lengths = [len(w) for w in words]
    word_len = len(words)
    return words, word_lengths, word_len
I like lesser code, without reaching the limit of horizontal space (79 chars).
We have enough vertical space and should use it.
Version which includes slices as well as while-loop and does not include .split().

To treat first, last element in iterable differently from others, set up variables on first loop and in else clause have two if-s, where second is for last element.

a = 'hello night sky'

for i, char in enumerate(a):
    if i == 0:
        words = list()
        start = 0
        
    else:
        if char == ' ':
            words.append(a[start:i])
            start = i + 1
            
        if i == len(a) - 1:
            words.append(a[start:])
    
w = len(words)

while words:
    word = words.pop(0)
    print(f'len = {len(word)} {word}')
print(f'There are {w} words')
Output:
len = 5 hello len = 5 night len = 3 sky There are 3 words
Reminder to a bunch of people on this thread: we're not supposed to do people's homework for them.
Pages: 1 2