[Tutor] Need Help!
Alex Kleider
akleider at sonic.net
Sat Oct 19 18:57:07 EDT 2019
On 2019-10-19 04:02, Ronald Walker via Tutor wrote:
>
> help(str.count)
>
>
>
> This is what I get:
>
>
>
> Help on method_descriptor:
>
>
>
> count(...)
>
> S.count(sub[, start[, end]]) -> int
>
>
>
> Return the number of non-overlapping occurrences of substring sub
> in
>
> string S[start:end]. Optional arguments start and end are
>
> interpreted as in slice notation.
>
>
>
> This is completely unintelligible to me.
Taciturn, yes, unintelligible, I'd have to disagree (and I am certainly
no computer scientist!)
It's explaining what the string method 'count' does and how to use it:
If you have a string, in this case it's represented by 'S',
you can call its count method as described: S.count(....
There is a mandatory parameter represented by sub- a string of your
choosing to specify the sub-string for which you are looking/counting.
Optional elements in computer jargon are often indicated by square
brackets- you'll get used to it.
So there are optional parameters within the square brackets:
'start' (defaults to 0 if not specified) allows you to specify where in
the string (S) you want to begin (if not at the beginning which is 0,
the default.)
'end' lets you also specify where in the string (S) to stop looking if
you don't wish to go to the end.
The '-> int' tells you that the method returns an integer. Another
convention to which you'll get accustomed.
The next line spells this out in a bit more detail: "..the number ..."
>
> How would I use this?
To give you an easy to find primer on how to use a particular feature of
the language.
> Return the number of non-overlapping occurrences of substring sub in
>
> string S[start:end]. Optional arguments start and end are
>
> interpreted as in slice notation.
>
Perhaps it'll be easier if an example is used:
s = "the cow jumped over the moon"
n = s.count('the')
The above should set n to 2, the number of instances of the substring
'the' in s.
latter_part = s.count('the', 4)
will set latter_part to 1 because you've told it to begin 4 characters
in, thus missing the first 'the'.
In essence s.count('the', 4, 16) (I think:-)
would yield the same as s[4:16].count('the').
If you find the last bit confusing it may be because you still haven't
learned about "slices".
Hope that helps.
More information about the Tutor
mailing list