Bottom Page

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?
michalmonday likes this post
Quote
#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.
Quote
#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
michalmonday likes this post
Quote
#4
Thanks everybody, that really helped and clarified the problem. Much appreciated.
Quote
#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
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  counting chars in string w/o len johneven 2 115 Mar-20-2019, 07:11 PM
Last Post: ichabod801
  slicing and counting words in a string Rudy 11 327 Mar-17-2019, 10:46 PM
Last Post: micseydel
  counting with dictionaries gitiya 3 448 Sep-07-2018, 01:25 PM
Last Post: gitiya
  Counting only letters in a string PraiseYAH 2 645 Jul-20-2018, 11:22 AM
Last Post: ichabod801
  How to exclude characters when counting digits juliabrushett 19 1,597 Jul-03-2018, 08:59 PM
Last Post: volcano63
  regex, counting occurences yanhto 4 694 May-06-2018, 10:20 PM
Last Post: killerrex
  Homework (counting iteration issue) Cardinal07 10 1,177 Feb-26-2018, 04:45 AM
Last Post: Larz60+
  [Discussion] Re. Substring counting Mekire 9 1,253 Jan-22-2018, 01:56 PM
Last Post: wavic
  making a program for counting hurmet 8 1,439 Oct-01-2017, 06:40 PM
Last Post: gruntfutuk

Forum Jump:


Users browsing this thread: 1 Guest(s)