Methods for String
.join(); .split(); .partition(); .strip(); .rstrip(); .lstrip(); .upper(); .lower(); .isupper(); .islower(); .isalpha(); .isalnum(); .isdecimal(); .isspace(); .istitle(); .startswith(); .endswith(); .rjust(); .ljust(); .center(); .find(); .count();
​
​
.split(): does the opposite - it’s called on a string value and returns a list of strings.
Inputs: a string
Returns: a list
E.g.1
>>> 'My name is Tiger'.split()
['My', 'name', 'is', 'Tiger']
E.g.2
>>> 'MyABC nameABC isABC Tiger'.split('ABC')
['My', ' name', ' is', ' Tiger']
E.g.3
>>> tiger = '''Dear Alice,
How have you been? I am fine.
There is a container in the fridge
that is labeled "Milk Experiment."
Please do not drink it.
Sincerely,
Bob'''
>>> tiger.split('\n')
['Dear Alice,', 'How have you been? I am fine.', 'There is a container in the
fridge', 'that is labeled "Milk Experiment."', '', 'Please do not drink it.',
'Sincerely,', 'Bob']
​
​
.partition(): split a string into the text before and after a separator string. This method searches the string it is called on for the separator string it is passed, and returns a tuple of three substrings for the “before,” “separator,” and “after” substrings.
input: string
output: tuple
​
>>> 'Hello, world!'.partition('w')
('Hello, ', 'w', 'orld!')
>>> 'Hello, world!'.partition('world')
('Hello, ', 'world', '!')
If the separator string you pass to partition() occurs multiple times in the string that partition() calls on, the method splits the string only on the first occurrence:
>>> 'Hello, world!'.partition('o')
('Hell', 'o', ', world!')
If the separator string can’t be found, the first string returned in the tuple will be the entire string, and the other two strings will be empty:
>>> 'Hello, world!'.partition('XYZ')
('Hello, world!', '', '')
You can use the multiple assignment trick to assign the three returned strings to three variables:
>>> before, sep, after = 'Hello, world!'.partition(' ')
>>> before
'Hello,'
>>> after
'world!'
​
.strip()
input: string
output: string
​
E.g.1
>>> spam = ' Hello world '
>>> spam.strip()
'Hello world'
E.g.2
>>> spam = 'SpamSpamBaconSpamEggsSpamSpam'
>>> spam.strip('ampS')
'BaconSpamEggs' # same result see below
NOTE:
Passing strip() the argument 'ampS' will tell it to strip occurrences of a, m, p, and capital S from the ends of the string stored in spam. The order of the characters in the string passed to strip() does not matter: strip('ampS') will do the same thing as strip('mapS') or strip('Spam').
>>> spam.strip('Spam')
'BaconSpamEggs' # same result
>>> spam.strip('mSap')
'BaconSpamEggs' # same result
.rstrip()
input: string
output: string
​
E.g.1
>>> spam = ' Hello world '
>>> spam.rstrip()
' Hello world'
E.g.2
>>> spam = 'SpamSpamBaconSpamEggsSpamSpam'
>>> spam.rstrip('Spam')
'SpamSpamBaconSpamEggs'
.lstrip()
input: string
output: string
E.g.1
>>> spam = ' Hello world '
>>> spam.lstrip()
'Hello world '
E.g.2
>>> spam = 'SpamSpamBaconSpamEggsSpamSpam'
>>> spam.lstrip('mSap')
'BaconSpamEggsSpamSpam'
​
.upper() - same use for .lower()
input: string
output: string
E.g.1
>>> tiger = 'Hello world!'
>>> tiger.upper()
'HELLO WORLD!'
>>> tiger
'Hello world!'
​
​
​
.isupper() - same use for .islower()
input: string
output: bool
E.g.1.
>>> tiger = 'Hello world!'
>>> tiger.islower()
False
>>> tiger.isupper()
False
>>> 'HELLO'.isupper()
True
​
​
​
.isalpha(): returns True if the string consists only of letters and isn’t blank
input: string
output: bool
​
E.g.1
>>> 'tiger'.isalpha()
True
>>> 'tiger123'.isalpha()
False
​
​
​
.isalnum(): returns True if the string consists only of letters and numbers and is not blank
input: string
output: bool
​
E.g.1
>>> 'tiger123'.isalnum()
True
>>> '123'.isalnum()
True
​
​
.isdecimal(): returns True if the string consists only of numeric characters and is not blank
input: string
output: bool
​
E.g.1
>>> '123'.isdecimal()
True
​
​
.isspace(): returns True if the string consists only of spaces, tabs, and newlines and is not blank
input: string
output: bool
E.g.1.
>>> ' '.isspace()
True
>>> 'hello '.isspace()
False
​
​
.istitle(): returns True if the string consists only of words that begin with an uppercase letter followed by only lowercase letters
input: string
output: bool
E.g.1
>>> 'This Is Title Case'.istitle()
True
>>> 'This Is Title Case 123'.istitle()
True
>>> 'This is Title Case'.istitle()
False
>>> 'This IS Title Case'.istitle()
False
​
​
.startswith() - same use for .endswith()
input: string
output: bool
>>> 'Hello, world!'.startswith('Hello')
True
>>> 'Hello, world!'.endswith('world!')
True
>>> 'abc123'.startswith('abcdef')
False
>>> 'abc123'.endswith('12')
False
>>> 'Hello, world!'.startswith('Hello, world!')
True
>>> 'Hello, world!'.endswith('Hello, world!')
True
​
​
.rjust()
input: string
output: string
​
E.g.1
>>> 'Hello'.rjust(10)
' Hello'
E.g.2
>>> 'Hello'.rjust(20, '*')
'***************Hello'
.ljust()
input: string
output: string
​
E.g.1
>>> 'Hello world'.ljust(20)
'Hello world
E.g.2
>>> 'Hello'.ljust(20, '-')
'Hello---------------'
.center()
input: string
output: string
E.g.1
>>> 'Hello'.center(20)
' Hello
E.g.2
>>> 'Hello'.center(20, '=')
'=======Hello========'
E.g.3
def printPicnic(itemsDict, leftWidth, rightWidth):
print('PICNIC ITEMS'.center(leftWidth + rightWidth, '-'))
for k, v in itemsDict.items():
print(k.ljust(leftWidth, '.') + str(v).rjust(rightWidth, '#'))
picnicItems = {'sandwitches': 4, 'apples': 12, 'cups': 4, 'cookies': 8000}
printPicnic(picnicItems, 12, 5)
printPicnic(picnicItems, 20, 6)
​
​
.find()
input: string
output: int
​
x = 'HEllo My name is John'
x.find("E") # finds the first occurrence's index position
1
x.find("HEllo")
0
x.find("My")
6 # My starts at index 6
.count()
input: string
output: int
​
x = 'hello my name is john'
x.count('m')
3 # m occurs 3 times
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​