- Declaring
strings
string
methods- Functions with
strings
- Strings are simply a collection of characters
- However, in Python, a
string
is a scalar type and is immutable - Strings are declared with single, double, or triple quotes.
- Mostly interchangeable. Single and double quotes only work with strings that span one line.
- Strings that span multiple lines need to be declared using triple quotes.
- We can use the addition operator to concatenate strings.
'My first string ' + 'My second string'
=>'My first stringMy second string'
- The multiplication operator will repeat the string
n
times.'Repeating string' * 3
=>'Repeating stringRepeating stringRepeating string'
- Just about any data type can be cast to a
string
.- Use the
string
casting function:str()
- Use the
- Not every
string
can be cast to another data type.int('a')
will not work
- String concatenation will only work between strings
- Need to cast any numeric variables to a
string
before concatenating.
- Need to cast any numeric variables to a
- Use the
join
string
method to create a singlestring
that contains all the elements of alist
.''.join(list)
- All the elements of the
list
must be strings for thejoin
method to work- You can use the
map
function to quickly cast all the elements of alist
to strings list(map(str, list))
- You can use the
- Use slice indexing (similar to a list) to access characters or subsequences of characters
string[index]
string[start:end]
string[start:end:skip]
- Check if a string is a substring of another string using the in keyword.
'str' in 'string'
- Returns a boolean
- The
replace
method is used to replace every occurence of a specified substring in the original substring with another substring- Syntax:
string.replace(old_substring, new_substring, 1)
- Syntax:
- The third argument here will limit the amount of times the
replace
method will be applied to only once.
string = 'I love to look at the moon'
string = string.replace('o', 'puppy', 2)
- The
enumerate
function can be used on strings:
for idx, ch in enumerate(string):
print(idx, ch)
Write a function that takes a list
like the following list
of column names and change any spaces in the column names to underscores. Return a modified list
with the updated names.
column_names = ['gender', 'longest absence from school', 'is enrolled', 'enlist', 'unemployed', 'filed for bankruptcy', 'school', 'peace corps']
column_names = ['gender', 'longest absence from school', 'is enrolled', 'enlist', 'unemployed', 'filed for bankruptcy', 'school', 'peace corps']
def add_underscores(feature_list):
new_list = feature_list.copy()
for idx, column in enumerate(new_list):
if ' ' in column:
new_list[idx] = column.replace(' ', '_')
return new_list
- A
string
can be cast to alist
using thelist
functionlist(string)
- The
list()
function will separate each character in thestring
into a new element in the returned list.
- Quick aside: Strings are immutable so methods will not change the
string
in place like with lists. - This method splits a
string
at each occurrence of a specified delimiter and will return a list with each split substring.string.split(delimiter)
- The delimiter will not be included in the returned
list
- The
lower
method will return astring
where all the letters are lowercase- Syntax:
string.lower()
- Syntax:
- The
upper
method will return astring
where all the letters are uppercase- Syntax:
string.upper()
- Syntax:
- The
swapcase
method will return astring
where all the letter’s cases are swapped- Syntax:
string.swapcase()
- Syntax:
- The
capitalize
method will return astring
where the first letter of thestring
will be capitalized and the rest will be lower case.- Syntax:
string.capitalize()
- Syntax:
- The
format
method will dynamically insert the variables passed in as parameters into the placeholders in astring
- Syntax:
'Inserting {} into this string'.format(variable_to_insert)
- Syntax:
f
strings will dynamically insert variables into strings. The difference is that we just need to place the variable name in the curly braces.- Syntax:
f'Inserting {variable_to_insert} into this string'
- Syntax:
Write a function that takes in a string. Return two parallel lists: one that contains the unique characters in the string and another that has the number of times that character appears in the original string.
s = 'This is a string, we want you to count how many times each unique character appears in this string!'
s = 'This is a string, we want you to count how many times each unique character appears in this string!'
def get_letter_count(string):
letters = []
counts = []
for let1 in string:
index = 0
if let1 not in letters:
letters.append(let1)
counts.append(0)
index = len(letters) - 1
else:
for idx, let2 in enumerate(letters):
if let1 == let2:
index = idx
break
counts[index] += 1
return letters, counts
letters, counts = get_letter_count(s)
for i in range(len(letters)):
print(f'{letters[i]}: {counts[i]}')
or
s = 'This is a string, we want you to count how many times each unique character appears in this string!'
def get_letter_count(string):
letters = []
counts = []
for let in string:
if let not in letters:
letters.append(let)
counts.append(1)
else:
counts[letters.index(let)] += 1
return letters, counts
letters, counts = get_letter_count(s)
for i in range(len(letters)):
print(f'{letters[i]}: {counts[i]}')
- The
count
method is used to count every occurrence of a specified substring in the original string- Syntax:
string.count('a')
- Syntax:
def get_letter_count(string):
letters = []
counts = []
for let in string:
if let not in letters:
letters.append(let)
counts.append(string.count(let))
else:
continue
return letters, counts