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
#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


Messages In This Thread
slicing and counting words in a string - by Rudy - Mar-13-2019, 10:59 PM
RE: slicing and counting words in a string - by scidam - Mar-13-2019, 11:54 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Counting the number of occurrences of characters in a string nsadams87xx 1 1,970 Jun-16-2020, 07:22 PM
Last Post: bowlofred
  counting chars in string w/o len johneven 2 4,163 Mar-20-2019, 07:11 PM
Last Post: ichabod801
  slicing words in a while loop missus_brown 4 3,136 Mar-18-2019, 01:13 AM
Last Post: scidam
  Using a function: splitting, joining, and slicing a string Drone4four 2 4,909 Dec-27-2018, 07:52 AM
Last Post: perfringo
  Counting only letters in a string PraiseYAH 2 4,603 Jul-20-2018, 11:22 AM
Last Post: ichabod801
  String Slicing fivestar 1 2,624 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