Counting Methods

Using maths in relation to Strings

Counting Methods

While we are thinking about the relevant index numbers that correspond to characters within strings, it is worth going through some of the methods that count strings or return index numbers. This can be useful for limiting the number of characters we would like to accept within a user-input form, or comparing strings. Like other sequential data types, strings can be counted through several methods.

Introduction

We’ll first look at the len() method which can get the length of any data type that is a sequence, whether ordered or unordered, including strings, lists, tuples, and dictionaries.

Let’s print the length of the string ss:

print(len(ss))
Show Output
12

The length of the string “Sammy Shark!” is 12 characters long, including the whitespace character and the exclamation point symbol.

Instead of using a variable, we can also pass a string right into the len() method:

print(len("Let's print the length of this string."))
Show Output
38

The len() method counts the total number of characters within a string.

len() Method

If we want to count the number of times either one particular character or a sequence of characters shows up in a string, we can do so with the str.count() method. Let’s work with our string ss = "Sammy Shark!" and count the number of times the character “a” appears:

print(ss.count("a"))
Show Output
2

We can search for another character:

print(ss.count("s"))
Show Output
0

Though the letter “S” is in the string, it is important to keep in mind that each character is case-sensitive. If we want to search for all the letters in a string regardless of case, we can use the str.lower() method to convert the string to all lower-case first.
Let’s try str.count() with a sequence of characters:

likes = "Sammy likes to swim in the ocean, likes to spin up servers, and likes to smile."
print(likes.count("likes"))
Show Output
3

In the string likes, the character sequence that is equivalent to “likes” occurs 3 times in the original string.

We can also find at what position a character or character sequence occurs in a string. We can do this with the str.find() method, and it will return the position of the character based on index number.

We can check to see where the first “m” occurs in the string ss:

print(ss.find("m"))
Show Output
2

The first character “m” occurs at the index position of 2 in the string “Sammy Shark!” We can review the index number positions of the string ss above.

Let’s check to see where the first “likes” character sequence occurs in the string likes:

print(likes.find("likes"))
Show Output
6

The first instance of the character sequence “likes” begins at index number position 6, which is where the character l of the sequence likes is positioned.

What if we want to see where the second sequence of “likes” begins? We can do that by passing a second parameter to the str.find() method that will start at a particular index number. So, instead of starting at the beginning of the string, let’s start after the index number 9:

print(likes.find("likes", 9))
Show Output
34

In this second example that begins at the index number of 9, the first occurrence of the character sequence “likes” begins at index number 34.

Additionally, we can specify an end to the range as a third parameter. Like slicing, we can do so by counting backwards using a negative index number:

print(likes.find("likes", 40, -6))
Show Output
64

This last example searches for the position of the sequence “likes” between the index numbers of 40 and -6. Since the final parameter entered is a negative number it will be counting from the end of the original string.

The string methods of len(), str.count(), and str.find() can be used to determine length, counts of characters or character sequences, and index positions of characters or character sequences within strings.

str.count() Method

Write a program that counts the number of characters in your name.

Write a program that counts the number of times the letter A appears in a sentence.

Write a program that finds the position of the letterA in a sentence but ignores the first and last two letters of the sentence.

2.5 Index and Slice Strings Task
Submit your completed task...
If you need to get in touch with Mr McG then this is the way….

What's your name?
image/svg+xml
Thank you [name], that's great.

Can I ask where you are from?
image/svg+xml
Hey [name], can I ask where you are from?
image/svg+xml
Hey [name], which class are you in?
image/svg+xml
OK [name], thanks for that.

I will need your email address so I can get back to you!!
image/svg+xml
Thanks [name], nearly there.

What can I help you with?
image/svg+xml

Thanks [name] from [whichclass][location], Mr McG will get your message…"
[message]"…and will get back to you ASAP via [email]. Catch you soon.

image/svg+xml
Thank you [name], Mr McG got your message.
Thanks [name], Mr McG will get back to you as soon as he can!!

Have a great day!!

K McGuinness - 2018

No personal data will be shared with any third party. The only data processed via this website is anonymous browsing data to best manage your user experience.