Python Forum
slicing and counting words in a string
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
slicing and counting words in a string
#1
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
Reply
#2
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.
Reply
#3
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

# ....................
Reply
#4
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.
Reply
#5
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)
Reply
#6
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?
Reply
#7
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'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#8
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.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#9
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
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#10
Reminder to a bunch of people on this thread: we're not supposed to do people's homework for them.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Counting the number of occurrences of characters in a string nsadams87xx 1 1,915 Jun-16-2020, 07:22 PM
Last Post: bowlofred
  counting chars in string w/o len johneven 2 4,090 Mar-20-2019, 07:11 PM
Last Post: ichabod801
  slicing words in a while loop missus_brown 4 3,080 Mar-18-2019, 01:13 AM
Last Post: scidam
  Using a function: splitting, joining, and slicing a string Drone4four 2 4,832 Dec-27-2018, 07:52 AM
Last Post: perfringo
  Counting only letters in a string PraiseYAH 2 4,524 Jul-20-2018, 11:22 AM
Last Post: ichabod801
  String Slicing fivestar 1 2,589 Nov-17-2017, 06:38 PM
Last Post: heiner55

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020