All letters are either lowercase or uppercase. We can change the case of characters in Python with the lower and upper methods.
String
casingMore 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.
upper()
on a string
that is already uppercased. Please see the isupper
and islower
methods.value = "Tree 5" # Uppercase the string. x = value.upper() print(x) # Lowercase the string. y = value.lower() print(y)TREE 5 tree 5
islower
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.
value1 = "ABC123" value2 = "abc123" # Method can be used in an if-statement. if value1.isupper(): print(1) # Call methods on both strings. print(value1.isupper()) print(value2.isupper()) print(value1.islower()) print(value2.islower())1 True False False True
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.
capitalize()
.capitalize()
does not change the existing string
in-place. It creates a new copy of the string
in memory and returns that.# An input string. s = "perls" # Capitalize and assign. s = s.capitalize() print(s)Perls
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
.
value = "the unbearable lightness of being" # Convert to title case. result = value.title() print(result)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.
value1 = "A Car" value2 = "A car" value3 = "a Car" value4 = "A123" value5 = "a123" # Test istitle method. print(value1.istitle()) print(value2.istitle()) print(value3.istitle()) print(value4.istitle()) print(value5.istitle())True False False True False
The islower
method can be used as an optimization. Before calling lower()
we can check whether the string
is already lowercase.
lower()
method on every iteration through the loop.islower()
before calling lower()
. It never calls lower()
because the string
is already lowercase.islower()
first is faster. This optimization will only work if your data is usually lowercase.import time value = "intuitive" print(time.time()) # Version 1: lower. i = 0 while i < 10000000: v = value.lower() i += 1 print(time.time()) # Version 2: islower and lower. i = 0 while i < 10000000: if not value.islower(): v = value.lower() i += 1 print(time.time())1384626116.650 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.
Text processing often involves normalizing the casing of strings. With the lower method and its friend upper()
, we do this.
Performance is improved in many programs by using islower()
and isupper()
. This reduces work but may introduce complexity.