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
#2
You are using the variable y for everything.
You want to use a separate variable to store the count and assign the count to 0 to begin with.
Reply
#3
(May-16-2019, 08:16 PM)tofif Wrote: 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.

Congrats on getting this far :) We're a very friendly place, and would love to help you along this journey.

Just like in other written languages, legibility is important. Being able to just glance at your code, and see what it does, is a huge boon to your productivity, as it prevents you from needing to re-read every line carefully to try to see what's happening.

To that end, as Yoriz stated, variable names are key to that. Here, I'll rewrite your snippet to try to demonstrate:
>>> text = "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?"
>>> spaces = 0
>>> for character in text:
...   if character == " ":
...     spaces += 1
...
>>> print(spaces)
37
>>> # or...
...
>>> text.count(" ")
37
Reply
#4
Thanks everybody, that really helped and clarified the problem. Much appreciated.
Reply
#5
Also it would have been better to have triple quotes around text.
Now you have extra space characters,that would not be there if written in normal way.
text = '''\
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?'''
>>> len([i for i in text if i == ' '])
34

# Or the same if use what nilmao showed you
spaces = 0
for character in text:
    if character == " ":
        spaces += 1

print(spaces) #--> 34
Reply


Forum Jump:

User Panel Messages

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