There are several built-in methods that allow us to easily make modifications to strings in Python. In this tutorial we will cover the .upper(), .lower(), .count(), .find(), .replace() and str() methods.
But first, let’s take a look at the len() method. While it’s not limited to strings, now is a good time to make the introduction. We use the built-in Python method, len(), to get the length of any sequence, ordered or unordered: strings, lists, tuples, and dictionaries. For example:
>>> s = “This string contains forty-two characters.”
>>> len(s)
42
.upper() & .lower()
The .upper() and .lower() string methods are self-explanatory. Performing the .upper() method on a string converts all of the characters to uppercase, whereas the lower() method converts all of the characters to lowercase.
>>> s = “Whereof one cannot speak, thereof one must be silent.”
>>> s
'Whereof one cannot speak, thereof one must be silent.'
>>> s.upper()
'WHEREOF ONE CANNOT SPEAK, THEREOF ONE MUST BE SILENT.'
>>> s.lower()
'whereof one cannot speak, thereof one must be silent.'
.count()
The .count() method adds up the number of times a character or sequence of characters appears in a string. For example:
>>> s = "That that is is that that is not is not is that it it is"
>>> s.count("t")
13
Why didn’t it count all of the t‘s? Because ‘T’ is a different character from ‘t’. So, if we want to count all of the t‘s.
>>> s = s.lower()
>>> s.count("t")
14
We can also count entire words, which, as we know, are sequences of characters:
s = "James while John had had had had had had had had had had had a better effect on the teacher"
>>> s.count("had")
11
.find()
We search for a specific character or characters in a string with the .find() method.
s = "On the other hand, you have different fingers."
>>> s. find("hand")
13
The results tell us that “hand” begins at the 13th position in the sequence.
>>> s.find("o")
7
But if we want to find the second “o” we need to specify a range.
>>> s.find("o", 8)
20
This begins searching at the 8th element and finds “o” at 20. You can also specificy an end to the range, and, like slicing, we can do so backwards:
>>> s.find("e", 20, -5)
26
.replace()
Let’s say we want to increase the value of a statement. We do so with the .replace() method. For example:
>>> s = "I intend to live forever, or die trying."
>>> s.replace("to", "three")
'I intend three live forever, or die trying.'
>>> s.replace("fore", "five")
'I intend to live fivever, or die trying.'
String Conversion
How does one become one with everything? With the str() method.
>>> one = str(1)
>>> one
'1'
Now that you are enlightened, you can stop learning Python and live in the moment. OR: You can learn about lists in our next chapter.