Session 18 – Python: Strings

Strings are lines of code encased in either single or double quotes.

#types of quotes that can be used
str = 'apple'
str = "apple"
str = """apple"""

#you can replace items in a string easily
str
new_str = str.replace("apple", "pear")
new_str

#you can obtain items from a string just like a list
str_2 = 'There are 10 apples in the store'
str_2[0]

str_2[1]

str_2[-1]

#you can slice a string
str_2[0:3]

str_2[1:2]

str_2[4:-1]

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
word_1 = 'hey! '
word_2 = 'hi! '
phrase_1 = 'How you doing?'
print(word_1 + word_2 + phrase_1)

#repeat words or phrases
fruit = "apples"
print(fruit*6)

#membership
fruit = "apples"
print("e" in fruit)

#no membership
fruit = "apples"
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
suspect_description = 'two short teenage males wearing one long brown coat and one brown hat'
str = 'one'
print(suspect_description.count(str))

str = 'two'
print(suspect_description.count(str))

str = 'brown'
print(suspect_description.count(str))

#the power of es-cap-e (escape)
print('\n')

print(r'\n')

#in-line es-cap-e
sentence = "It was \"him\" your honor!"

#notice how the example below will not work
sentence = "It was "him" your honor!"

#capitalize first letter
name_1 = 'TOM'
print(name_1.capitalize())

name_2 = 'tom'
print(name_2.capitalize())

#lowercase letters
print(name_1.lower())

#uppercase letters
print(name_2.upper())

#swapcase of characters
name_3 = 'ToM HollAnD'
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
term1 = "highlands"

#check if all lowercase
print(term1.islower())

#check if all uppercase
print(term1.isupper())

#check if characters are decimals
term2 = '20.2'
print(term2.isdecimal())

term2 = '202'
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())

term3 = 'wow5'
print(term3.isalpha())

#same as alpha but includes decimal digits
print(term2.isalnum())

print(term3.isalnum())

18.5 Padding strings

#padding with rjust, rightsided
readme = "Start App"
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))

readme_aug = '-Start App-'
print(readme_aug.zfill(25))

18.6 Search for words in string and output location

#find function
house = "green two-story home"
str = "two"
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
smthg_he_wrote = 'I am moving out'
print(smthg_he_wrote.replace('out','away'))

#split
print(smthg_he_wrote.split())

#splitlines
print(smthg_he_wrote.splitlines())

smthg_he_wrote_2 = 'I am definately \nwithout a doubt \nmoving out'
print(smthg_he_wrote_2)

print(smthg_he_wrote_2.splitlines())

#join
alphabet = ('a','b','c')
print(' '.join(alphabet))

print('+'.join(alphabet))

#remove leftsided whitespaces
extra_spaces = '     hi     hey     '
print(extra_spaces.lstrip())

#remove rightsided whitespaces
print(extra_spaces.rstrip())

#remove any type of character, not just whitespaces
extra_char = '-----hi-----'
print(extra_spaces.rstrip('-'))

#print length of string
print(len(extra_spaces))