Python string methods
Python built-in methods that allow us to easily modifications to strings. In this tutorial we will cover the python string methods : upper(), lower(), count(), find(), replace() and str() methods.
String Methods
upper() and lower()
The upper() method on a string converts all of the characters to uppercase. and the lower() method converts all of the characters to lowercase.
>>> str = "Python String Methods" >>> str.upper() output : 'PYTHON STRING METHODS' >>> str.lower() output : 'python string methods'
count()
The count() technique includes the occasions a character or succession of characters shows up in a string
>>> str = "Python String Methods" >>> str.count("o") output : 2
find()
We search for a specific character or characters in a string with the find() method.
>>> str = "Python String Methods" >>> str.find("Methods") output : 14
The results tell us that Methods begins at the 14th position in the sequence.
>>> str.find("o") output : 4
But if we want to find the second “o” we need to specify a range.
>>> str.find("o", 5) output : 18
replace()
We want to increase the value of a statement. We do so with the replace() method.
>>> str = "Python String Methods" >>> str.replace("Methods", "Method") output : 'Python String Method'
len()
And utilize the implicit Python technique, len(), to get the length of any unordered, sequence or ordered.
>>> str = "Python string methods" >>> len(str) output : 21