Using maths in relation to Strings
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.
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:
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."))
The len() method counts the total number of characters within a string.
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:
We can search for another character:
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"))
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:
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:
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:
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))
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.
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.
Thanks [name] from [whichclass][location], Mr McG will get your message…"
[message]"…and will get back to you ASAP via [email]. Catch you soon.
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.