Strings

  • string: sequence of characters with contained within single or double quotes

  • strings are immutable

    • index works in the same manner as in a list

cat_name = "Baxter"
print(cat_name[0]) 

#output --> B
  • slicing a string: return a chunk of text between the first and last index

cat_name = "Baxter"

print(cat_name[1:4])
#output --> axt

print(cat_name[:3])
#output --> Bax

print(cat_name[4:])
#output --> er

print(cat_name[-1])
#output --> r

print(cat_name[-4:-2]
#output --> xt
  • concatenate = combine two or more existing strings

input_a = "ABC"
input_b = "123"
combined = input_a + input_b
print(combined)

#output --> ABC123

favorite_team = "Yankees"
print("My favorite team is the " + favorite_team)

#output --> "My favorite team is the Yankees
  • len() can be used in the index to slice text based on the size of the string

favorite_dino = "Tyrannosaurus Rex"
print(len(favorite_dino)

#output --> 17 (counts the space as well)

last_char = favorite_dino[len(favorite_dino)]
print(last_char)
#this is an error, because you can't index of the len because it goes past the last character

last_char2 = favorite_dino[len(favorite_dino) -1]
print(last_char2)
#using the -1, will return the final letter (x)

last_char3 = favorite_dino[len(favorite_dino)-4]
print(last_char3)
#this would print the " "

last_char4 = favorite_dino[len(favorite_dino)-3:len(favorite_dino)-1]
print(last_char4)
#this would print the "Re", but not the "x"
  • escape characters - special way to include symbols, line breaks, etc. by including a \ in the string

#if using single quotes, use \' for apostrophes
quote = 'I don\'t like grapefruit'
print(quote)

#to create a new line, use \n
quote2 = 'I don\'t like grapefruit \n or grapes.'
print(quote2)
#output is two lines, with the line break after grapefruit

  • iterate through a string using for loops

def get_length(word):
  counter = 0
  for letter in word:
    counter += 1
  return counter

print(get_length("Stegosaurus"))
#output = 11

def print_letters(word):
  for letter in word:
    print(letter)

print_letters("Stegosaurus")
#output --> S on first line, t on 2nd, etc.

def print_letters(word):
  for letter in word:
    print(letter)

print_letters("Stegosaurus"[1:3])
#output --> first line will be the t, then 2nd the e
  • you can also use conditionals such as “if”

def letter_check(word, letter):
  for character in word:
    if character == letter:
      return True
  return False

print(letter_check("Stegosaurus", "g"))
#this will return True
print(letter_check("Stegosaurus", "y"))
#this will return False

###############################

def contains(big_string, little_string):
  if little_string in big_string:
    return True
  return False

print(contains("baseball", "ball"))
#this will be True
print(contains("baseball", "foot"))
#this will be False

def common_letters(string_one, string_two):
  common = []
  for letter in string_one:
    if (letter in string_two) and not (letter in common):
      common.append(letter)
  return common

print(common_letters("baseball", "Bandit"))
# this will be ["a"]  
print(common_letters("baseball", "basketball"))    
# this will return ["b", "a", "s", "e", "l"]
print(common_letters("baseball", "zoo"))
# this will return the empty set []