Strings and Lists

Strings and lists are two of the most common types of data you work with in Python. Strings enable you to represent text, and lists enable you to manipulate collections of data, be it lists of numbers, lists of strings, or even lists of lists! While they serve very different purposes, these two types also have a lot in common. Specifically, they are both sequences. Indeed, we can think of strings as a sequence of individual letters.

To help you learn both the common and unique features of strings and lists, we give examples here of the most common ways you’ll use strings and lists in Lab 3 and beyond.

Creating Strings and Lists

StringsLists
message = 'Hello!'
message
'Hello!'
numbers = [ 1, 2, 3 ]
numbers
[1, 2, 3]
wordle = 'happy'
wordle
'happy'
words = [ 'moo', 'cow', 'woof' ]
words
['moo', 'cow', 'woof']
nothing = ''   # The empty string
nothing
''
noElems = [ ]    # The empty list
noElems
[]

Lengths of Strings and Lists

StringsLists
wordle = 'happy'
len(wordle)
5
numbers = [ 1, 2, 3 ]
len(numbers)
3

Combining Strings and Lists

StringsLists
message = 'Hel' + 'lo!'
message
'Hello!'
numbers = [ 1, 2, 3 ] + [ 4, 5, 6 ]
numbers
[1, 2, 3, 4, 5, 6]
wordle = 'happy'
doubleWordle = wordle + ' and ' + wordle
doubleWordle
'happy and happy'
words = [ 'moo', 'cow', 'woof' ]
manyWords = words + words
manyWords
['moo', 'cow', 'woof', 'moo', 'cow', 'woof']

Appending to a String or List

StringsLists
message = 'Hello'
message += '!'
message
'Hello!'
numbers = [ 1, 2, 3 ]
numbers += [ 4 ]
numbers
[1, 2, 3, 4]
numbers = [ 1, 2, 3 ]
numbers.append(4)  # alternate way to append
numbers
[1, 2, 3, 4]

What’s in a String or List?

StringsLists
message = 'Hello'
'l' in message
True
numbers = [ 1, 2, 3 ]
3 in numbers
True
message = 'Hello'
'x' in message
False
numbers = [ 1, 2, 3 ]
33 in numbers
False
message = 'Hello'
'y' not in message
True
numbers = [ 1, 2, 3 ]
3 not in numbers
False

Iterating over Strings and Lists

StringsLists
message = 'Hello'
for letter in message:
  print(letter)
H
e
l
l
o
numbers = [ 1, 2, 3 ]
for number in numbers:
  print(number)
1
2
3
# Build a new list containing all the vowels 
# from `message`.

message = 'Hello'
vowels = ''

for letter in message:
  if letter in [ 'a', 'e', 'i', 'o', 'u']:
    vowels += letter

vowels
'eo'
# Build a new list containing the square 
# of all odd numbers in `numbers`.

numbers = [ 1, 2, 3, 4, 5, 6 ]
squares = [ ]

for number in numbers:
  if number % 2 == 0:
    square = number * number
    squares += [ square ]

squares
[4, 16, 36]

Accessing Elements with []

StringsLists
message = 'Hello'
message[0]
'H'
numbers = [ 5, 6, 7 ]
numbers[0]
5
message = 'Hello'
message[4]
'o'
numbers = [ 5, 6, 7 ]
numbers[2]
7
message = 'Hello'
message[-1]
'o'
numbers = [ 5, 6, 7 ]
numbers[-1]
7

Slicing Strings and Lists

StringsLists
message = 'Hello'
message[1:3]
'el'
numbers = [ 5, 6, 7, 8 ]
numbers[0:2]
[5, 6]
message = 'Hello'
message[1:]
'ello'
numbers = [ 5, 6, 7, 8 ]
numbers[:2]
[5, 6]
message = 'abcdefghij'
message[0::2]
'acegi'
numbers = [ 5, 6, 7, 8, 9, 10 ]
numbers[1:4:2]  
[6, 8]

Handy String Operations

str.lower(): Convert to Lowercase
message = 'Hello'
message.lower()
'hello'
str.upper(): Convert to Uppercase
message = 'Hello'
message.upper()
'HELLO'
str.islower(): Test if a String is All Lowercase
message = 'hello'
message.islower()
True
message = 'Hello'
message.islower()
False
str.isupper(): Test if a String is All Uppercase
message = 'Hello'
message.isupper()
False
message = 'HELLO'
message.isupper()
True
==: Test if Two Strings Are the Same
message = 'Hello'
message == 'Hello'
True
message = 'Hello'
other = 'Bye'
message == other
False
!=: Test if Two Strings Are Different
message = 'Hello'
message != 'Hello'
False
message = 'Hello'
other = 'Bye'
message != other
True
str.isalpha(): Test if a String Only Contains Letters from Alphabet
message = 'Hello'
message.isalpha()
True
message = 'Hello22'
message.isalpha()
False