Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
counting spaces
#1
Greetings everybody,
First off, I'm a 71 year old retired male at home trying to learn python. No schools involved. I just thought this was the best forum to start. I'm using a college testbook to study with.
I'm stuck with this question: "Write a loop that counts the number of space characters in a string.
Recall that the space character is represented as ' '."

 y = '  '
         for y in "To be, or not to be? That is the question." \
         " Whether ’tis nobler in the mind to suffer" \
         "the slings and arrows of outrageous fortune," \
         " or to take arms against a sea of troubles, " \
         "and, by opposing, end them?" :  

           if y ==' '
              y = y + 1        
 
         print(y) 
The only thing i get when running is either the string or an error saying something about mixing int's
with strings. How do you get a count in here??? I'm probably missing the point on something.
I did google and found this: print(y.count(' ')) Works well,but I still am missing the point on running this through a loop. How do I get a count in here?
Reply


Messages In This Thread
counting spaces - by tofif - May-16-2019, 08:16 PM
RE: counting spaces - by Yoriz - May-16-2019, 08:25 PM
RE: counting spaces - by nilamo - May-16-2019, 08:58 PM
RE: counting spaces - by tofif - May-16-2019, 09:14 PM
RE: counting spaces - by snippsat - May-17-2019, 02:36 PM

Forum Jump:

User Panel Messages

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