Strings Basics

Intro to Python Strings

A string is a sequence - an ordered collection of values.

Strings are always in single or double quotes and often viewed as text blobs. Some examples of strings in Python programs are:

  • song lyrics
  • names of people
  • conversation text

Here is an example of a string.

computer = "apple"

Note a string is a data type of a variable - just as an int and float are other example of data types.


A string is a sequence

Just like any Python sequence, you can access the characters.

You can access a single character. Here we access the first item in the sequence - the letter a.

computer = "apple"

The expression in brackets above is considered an index. The index of any sequence in computer science starts at 0.

Get the length of the sequence

An important built-in Python function len returns the number of characters in a sequence.

computer = "apple"

This variable computer is assigned to the string apple and that string has 5 letters. So the length of this sequence should be 5.


Traverse with a for loop

In Python, you may need to perform computations on each character in a string.

To do so, select each character, do some computation and continue until the end. This pattern is called a traversal.

Commonly, you'll traverse a string with a for loop. Let's do that to our variable computer.

We'll count the number of times the letter p appears. We should get an answer of 2 since there are two p's in the string apple.

count_of_p = 0

for letter in computer:
    if letter == 'p':
        count_of_p += 1

In the snippet above, the computation is called a counter because count_of_p is assigned to the int value of 0 and stores the value for the count of the letter p in our string.

Strings are immutable

Strings are immutable - you cannot change an existing string in place.

Let's say we have a variable greeting below that's a string.

greeting = "Hello Dan"

We cannot change this greeting variable.

We can create a new_greeting variable that is a variation of the original.

new_greeting = greeting + "!"
'Hello Dan!'

The in operator

The word in is a boolean operator that takes two strings and returns True if the first string appears as a substring in the second.

The string "a" is contained in "banana".

"a" in "banana"

The string "bat" is not contained in the string "banana".

"bat" in "banana"

Compare strings

The == operator works on strings to compare if they're equal. If the two are equal, it returns True, otherwise, it returns False.

"dan" == "dan"

Strings are case sensitive so an uppercase and lowercase letter are different.

"dan" == "Dan"
"dan" == "mary"

We can also compare strings for alphabetical order.

We know the letter a appears in the alphabet before the letter d. So a is less than d.

"a" < "d"

We can compare whole words too. Think of this as evaluating which word you'd find first in a dictionary book.

Letter c comes before letter d.

"cat" < "dog"

The expression below is False becuase we find words that start with ca before words with co in a dictionary book since a is alphabetically before o.

"cat" > "cow"