Session 18 – Python: Strings
Strings are lines of code encased in either single or double quotes.
#types of quotes that can be used
= 'apple'
str = "apple"
str = """apple"""
str
#you can replace items in a string easily
str= str.replace("apple", "pear")
new_str
new_str
#you can obtain items from a string just like a list
= 'There are 10 apples in the store'
str_2 0]
str_2[
1]
str_2[
-1]
str_2[
#you can slice a string
0:3]
str_2[
1:2]
str_2[
4:-1] str_2[
Strings cannot be changed once they are assigned to a variable. You can change them via variable reassignment or you can simply delete them with the ‘del’ command followed by the variable.
18.1 String Operators
#cat strings together
= 'hey! '
word_1 = 'hi! '
word_2 = 'How you doing?'
phrase_1 print(word_1 + word_2 + phrase_1)
#repeat words or phrases
= "apples"
fruit print(fruit*6)
#membership
= "apples"
fruit print("e" in fruit)
#no membership
= "apples"
fruit print("e" not in fruit)
18.2 Other String Modifiers
#iteration through a string
for fruit_letter in fruit: print(fruit_letter, end ="")
for fruit_letter in fruit: print(fruit_letter)
#count number of occurances
= 'two short teenage males wearing one long brown coat and one brown hat'
suspect_description = 'one'
str print(suspect_description.count(str))
= 'two'
str print(suspect_description.count(str))
= 'brown'
str print(suspect_description.count(str))
#the power of es-cap-e (escape)
print('\n')
print(r'\n')
#in-line es-cap-e
= "It was \"him\" your honor!"
sentence
#notice how the example below will not work
= "It was "him" your honor!"
sentence
#capitalize first letter
= 'TOM'
name_1 print(name_1.capitalize())
= 'tom'
name_2 print(name_2.capitalize())
#lowercase letters
print(name_1.lower())
#uppercase letters
print(name_2.upper())
#swapcase of characters
= 'ToM HollAnD'
name_3 print(name_3.swapcase())
#title, capitalize first letters of each word
print(name_3.title())
18.3 Format-Characters in Strings
print("Last Name: %s,\nFirst Name: %s,\nLast Name: %s" % ('Bond','James','Bond'))
Most common format characters: %s = string %d = decimal %c = character %e = exponential notation %E = exponential notation %f = floating-point real number
18.4 Comparisons with Strings
#check case type
= "highlands"
term1
#check if all lowercase
print(term1.islower())
#check if all uppercase
print(term1.isupper())
#check if characters are decimals
= '20.2'
term2 print(term2.isdecimal())
= '202'
term2 print(term2.isdecimal())
#check if characters are digits
print(term2.isdigit())
#check if characters are numeric
print(term2.isnumeric())
#check if string contains at least one character, all alphabetic
print(term2.isalpha())
= 'wow5'
term3 print(term3.isalpha())
#same as alpha but includes decimal digits
print(term2.isalnum())
print(term3.isalnum())
18.5 Padding strings
#padding with rjust, rightsided
= "Start App"
readme print(readme.rjust(15))
print(readme.rjust(15, '~'))
#padding with ljust, leftsided
print(readme.ljust(15))
print(readme.ljust(15, '~'))
#padding with center, centered
print(readme.center(25))
#padding with zfill, fills after a + or -
print(readme.zfill(25))
= '-Start App-'
readme_aug print(readme_aug.zfill(25))
18.6 Search for words in string and output location
#find function
= "green two-story home"
house = "two"
str print(house.find(str))
print(house.find(str,5))
#what happens below, when we pass the location of 'two'
print(house.find(str,10))
#for this case above, we should use index instead of find
print(house.index(str,10))
#count
print(house.count(str))
print(house.count(str, 10))
18.7 Seek and replace in strings
#replace
= 'I am moving out'
smthg_he_wrote print(smthg_he_wrote.replace('out','away'))
#split
print(smthg_he_wrote.split())
#splitlines
print(smthg_he_wrote.splitlines())
= 'I am definately \nwithout a doubt \nmoving out'
smthg_he_wrote_2 print(smthg_he_wrote_2)
print(smthg_he_wrote_2.splitlines())
#join
= ('a','b','c')
alphabet print(' '.join(alphabet))
print('+'.join(alphabet))
#remove leftsided whitespaces
= ' hi hey '
extra_spaces print(extra_spaces.lstrip())
#remove rightsided whitespaces
print(extra_spaces.rstrip())
#remove any type of character, not just whitespaces
= '-----hi-----'
extra_char print(extra_spaces.rstrip('-'))
#print length of string
print(len(extra_spaces))