String objects have a variety of methods we can use to save time and add functionality. Lets explore some of them in this lecture:
s = 'hello world'
We can use methods to capitalize the first word of a string, change cases to upper and lower case strings.
# Capitalize first word in string
s.capitalize()
'Hello world'
s.upper()
'HELLO WORLD'
s.lower()
'hello world'
s.count('o')
2
s.find('o')
4
The center() method allows you to place your string 'centered' between a provided string with a certain length. Personally, I've never actually used this in code as it seems pretty esoteric...
s.center(20,'z')
'zzzzhello worldzzzzz'
expandtabs() will expand tab notations \t into spaces:
'hello\thi'.expandtabs()
'hello hi'
These various methods below check it the string is some case. Lets explore them:
s = 'hello'
isalnum() will return True if all characters in S are alphanumeric
s.isalnum()
True
isalpha() wil return True if all characters in S are alphabetic
s.isalpha()
True
islower() will return True if all cased characters in S are lowercase and there is at least one cased character in S, False otherwise.
s.islower()
True
isspace() will return True if all characters in S are whitespace.
s.isspace()
False
istitle() will return True if S is a title cased string and there is at least one character in S, i.e. uppercase characters may only follow uncased characters and lowercase characters only cased ones. Return False otherwise.
s.istitle()
False
isupper() will return True if all cased characters in S are uppercase and there is at least one cased character in S, False otherwise.
s.isupper()
False
Another method is endswith() which is essentially the same as a boolean check on s[-1]
s.endswith('o')
True
Strings have some built-in methods that can resemble regular expression operations. We can use split() to split the string at a certain element and return a list of the result. We can use partition to return a tuple that includes the separator (the first occurrence) and the first half and the end half.
s.split('e')
['h', 'llo']
s.partition('e')
('h', 'e', 'llo')
s
'hello'
Great! You should now feel comfortable using the variety of methods that are built-in string objects!