Python Lower and Upper: Capitalize StringUse the lower, upper, islower and isupper methods. Call title and capitalize.
Lower, upper. All letters are either lowercase or uppercase. We can change the case of characters in Python with the lower and upper methods.
String casing. More powerful methods such as capitalize and title() also exist. We cover the basic methods (upper and lower) first, and then explore isupper and islower.
Often we need to uppercase (or lowercase) strings. With the upper and lower methods, we apply the needed transformations. These methods do not affect non-letter characters.
Tip: We never need to call upper() on a string that is already uppercased. Please see the isupper and islower methods.
Python program that uses upper, lower
value = "Tree"
# Uppercase the string.
x = value.upper()
# Lowercase the string.
y = value.lower()
Is a string already uppercase or lowercase? We can tell this easily with the isupper and islower methods. These two methods are often used with if-statements.
Also: Please see the performance test. Is lower is used to improve speed. With it, we can avoid copying strings.
Python program that uses isupper, islower
value1 = "ABC123"
value2 = "abc123"
# Method can be used in an if-statement.
# Call methods on both strings.
This method affects only the first letter in a string. It changes it to be uppercase. If the first character is not a letter, nothing happens.
Here: Please notice that the variable "s" is assigned to the result of capitalize().
Tip: This means capitalize() does not change the existing string in-place. It creates a new copy of the string in memory and returns that.
Python program that uses capitalize
# An input string.
s = "perls"
# Capitalize and assign.
s = s.capitalize()
Strings have a title method. This method is not smart. But it does help with many common strings. It capitalizes each word in a string.
Warning: Title will capitalize words that are not supposed to be capitalized, like "of" and "and."
Thus: A custom implementation (not the title method) would be needed if your requirements are more complex.
Python program that uses title string method
value = "the unbearable lightness of being"
# Convert to title case.
result = value.title()
The Unbearable Lightness Of Being
This is a less-useful method. It scans a string and determines if the string has capital letters for each word. It can be used to avoid calling title() on strings.
Note: As with the title method, istitle will become confused on certain words. It requires all words, even "and" to be capitalized.
Python program that uses istitle
value1 = "A Car"
value2 = "A car"
value3 = "a Car"
value4 = "A123"
value5 = "a123"
# Test istitle method.
The islower method can be used as an optimization. Before calling lower() we can check whether the string is already lowercase.
And: If the string is already lowercased, we can avoid calling lower. This avoids creating a new string.
Caution: If the majority of your strings are not lowercase, this check will slow down a program. The islower method has a cost too.
Result: The code that checks islower() first is more than twice as fast. This is an unusual data set. It is not real-world data.
However: Many programs add calls to lower just in case. Usually these calls have no effect. This optimization would benefit these programs.
Python program that benchmarks islower, lower
value = "intuitive"
# Version 1: lower.
i = 0
while i < 10000000:
v = value.lower()
i += 1
# Version 2: islower and lower.
i = 0
while i < 10000000:
if not value.islower(): v = value.lower()
i += 1
1384626122.033 lower(): 5.38 s
1384626124.027 islower() and lower(): 1.99 s
We can use a dictionary to avoid lowercasing the same string many times. This can speed up the lowercase operation—the cache may be 10 times faster.Lower Dictionary
A summary. Text processing often involves normalizing the casing of strings. With the lower method and its friend upper(), we do this.
Testing cases. Performance is improved in many programs by using islower() and isupper(). This reduces work but may introduce complexity.
© 2007-2019 Sam Allen. Every person is special and unique. Send bug reports to email@example.com.