Strings#

What you will learn in this lesson:

  • String operations

  • Subsetting a string

  • String methods

  • Formatting strings

What is a string?#

We already saw in a previous lesson that strings are just a sequence of characters, enclosed in quotes (’) or apostrophes (“).

In some aspects, strings are similar to those data structures (e.g. lists) we are going to study in our next lesson. The main difference is that strings only store characters, whereas data structures can store and mix different types of data.

Studying strings first will give you a nice and powerful introduction to the different things we can do with data structures.

string_var = "This is a string variable"

print(type(string_var))
<class 'str'>
# Strings can be just one character
string_var = "a"
print(type(string_var))
<class 'str'>

String Operators#

concatenation +: To join strings.

string1 = 'My name is Iñigo Montoya! '
string2 = 'You killed my father! '
string1 + string2
'My name is Iñigo Montoya! You killed my father! '
string1 + string2 + 'Prepare to die!'
'My name is Iñigo Montoya! You killed my father! Prepare to die!'
print(string1 + string2 + 'Prepare to die!')
My name is Iñigo Montoya! You killed my father! Prepare to die!

repetition *: To repeat strings.

string1 * 2
'My name is Iñigo Montoya! My name is Iñigo Montoya! '
# '\n' moves to the next line (like hitting the return button)
print((string1 + '\n') * 5)
My name is Iñigo Montoya! 
My name is Iñigo Montoya! 
My name is Iñigo Montoya! 
My name is Iñigo Montoya! 
My name is Iñigo Montoya! 
Info: '\n' is a sequence that breaks the line

Subsetting a string#

subsetting refers to selecting parts of the string. As we will see in the next lesson, subsetting works similarly in many other structures (e.g. lists).

Indexing#

We can access individual elements using brackets [ ] and passing the index for the character we want to select.

Python uses zero-based indexing, which means that for a string mystring, mystring[0] references the first element, mystring[1] references the second element, etc

# This gives the first element
string1[0]
'M'
# This gives the secong element
string1[1]
'y'

Negative indices go in reverse:

# This gives the last element
string1[-1]
' '
# This gives the second-to-last element
string1[-2]
'!'
Warning: Remember!! In Python, indexing starts at ZERO.

Slicing#

We have several options of slicing a string:

  • mystring[start:stop] characters start to stop-1.

  • mystring[start:] characters start to the rest of the string. We can omit the index corresponding to the length of the string.

  • mystring[:stop] characters from the beginning to stop-1. We can omit the index 0.

  • mystring[:] a copy of the whole string. We can omit both first and last index of the string.

# extract the first 2 characters
string1[0:2] 
'My'
# Like we mentioned, we can omit the index 0 when slicing
string1[:2] 
'My'

Likewise, for any string of length N:

mystring[:-n] will return all characters except the last n ones.
mystring[-n:] will return the last n characters from index N-n to N-1

# extract the last 2 characters
string1[-2:]
'! '
# extract all characters but the last one
string1[:-1]
'My name is Iñigo Montoya!'

To get the last index of an string, with can use the len function, which, as its name suggests, gives the length of the string, i.e. the number of characters of the string.

# This returns the number of character
len(string1)
26
string1[11:len(string1)]
'Iñigo Montoya! '
# Although we can omit the end index if we want to select up to the end of the string
string1[11:]
'Iñigo Montoya! '
Info: len is the same function that we will use to get the number of elements in other data structures like lists, tuples and dictionaries (We will see this soon).

So far we have selected consecutive characters, but we can also specify the step between selected characters. In this case we would use: mystring[start:stop:end]

# This would select every two characters
string1[::2] 
'M aei ñg oty!'
# This reverses the string
string1[::-1] 
' !ayotnoM ogiñI si eman yM'

Operator in: check membership#

We can check whether particular strings exist within a given string. It returns a boolean if exists.

"Iñigo" in string1
True
"Javi" in string1
False

String Methods#

One important thing we have to understand is that EVERYTHING in Python is an object. And when we say everything is everything – numbers, strings, lists, etc.

Objects in Python have two key characteristics: attributes to store data, and methods to perform specific operations with the objects. Both are called using a dot (.) after the name of the object.

We will come back to this in more detail when we study classes, but for now it is important that you get this idea.

  • startswith and endswith: Check whether the string starts and ends by a given string.

string1.startswith('o')
False
# It does not need to by a single character
string1.startswith('My')
True
string1.endswith('! ')
True
  • upper and lower: Convert string to upper or lower case.

string1.upper()
'MY NAME IS IÑIGO MONTOYA! '
string1.lower()
'my name is iñigo montoya! '
  • strip: Remove leading/trailing whitespace.

string1.strip()
'My name is Iñigo Montoya!'
  • replace: Replace parts of a string.

string1.replace("Iñigo Montoya", "Javier Rasero")
'My name is Javier Rasero! '
  • split: Split a string based on a given character. It returns a list. String Formatting

# Here based on "I"
string1.split("I")
['My name is ', 'ñigo Montoya! ']
# If nothing is specified, it splits based on blank space
string1.split()
['My', 'name', 'is', 'Iñigo', 'Montoya!']
  • join: join a list of strings by a given character.

" ".join(["Hello", "class"])
'Hello class'

String Formatting#

String formatting allows you to create strings that include variables or expressions, which is useful for generating dynamic messages, or any text that needs incorporating variable information. We are going to study the two most common ways for this:

  • f-strings (Formatted String Literals): You need to prefix the string with f and then any variable inside curly braces {}:

my_name = "Javier Rasero"

print(f"my name is {my_name}")
my name is Javier Rasero
tall = 176

print(f"And I am {tall} cm tall.")
And I am 176 cm tall.
# We can combine both

print(f"my name is {my_name} and I am {tall} cm tall.")
my name is Javier Rasero and I am 176 cm tall.
  • Using the format method:

# Here we don't need to prefix "f". This is a method inherent to strings
print("my name is {} and I am {} cm tall.".format(my_name, tall))
my name is Javier Rasero and I am 176 cm tall.

Immutability#

Python strings are immutable - cannot reassign elements. This feature applies to other data structures too (next lesson).

string1[0] = 'Y'
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[37], line 1
----> 1 string1[0] = 'Y'

TypeError: 'str' object does not support item assignment

Practice exercises#

Exercise 8

1 - Create two strings you like. 2 - Concatenate them using the + operator. Save the result in a new variable. 3 - Extract some part of the combined string using slicing. Save the result in a new variable. 4 - Print the final concatenated string and the extracted substring.

# Start your answers here

Exercise 9

1 - Create a string variable with the characters you like. 2 - Reverse the string using slicing. Save the result in a new variable. 3 - Replace a character in the reversed string using the .replace() method. Save the result in a new variable. 4 - Print the strings created in 2 and 3.

# Start your answers here