Python Beginner Concepts Tutorial

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.

In [49]:
computer = "apple"

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

In [50]:
type(computer)
Out[50]:
str

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.

In [39]:
computer = "apple"
In [40]:
computer[0]
Out[40]:
'a'

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.

In [41]:
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.

In [42]:
len(computer)
Out[42]:
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.

In [43]:
count_of_p = 0

for letter in computer:
    if letter == 'p':
        count_of_p += 1
In [48]:
count_of_p
Out[48]:
2

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.

In [45]:
greeting = "Hello Dan"

We cannot change this greeting variable.

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

In [46]:
new_greeting = greeting + "!"
In [47]:
new_greeting
Out[47]:
'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".

In [51]:
"a" in "banana"
Out[51]:
True

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

In [52]:
"bat" in "banana"
Out[52]:
False

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.

In [53]:
"dan" == "dan"
Out[53]:
True

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

In [56]:
"dan" == "Dan"
Out[56]:
False
In [57]:
"dan" == "mary"
Out[57]:
False

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.

In [58]:
"a" < "d"
Out[58]:
True

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.

In [59]:
"cat" < "dog"
Out[59]:
True

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.

In [60]:
"cat" > "cow"
Out[60]:
False