String basics¶
Welcome to your first tutorial on string processing with Python. In this tutorial, you will learn how Python represents text and discover the fundamental operations you can perform on strings.
Time commitment: 15–20 minutes
Prerequisites:
- Python 3.12 or later installed
- Basic familiarity with running Python code
Learning objectives¶
By the end of this tutorial, you will be able to:
- Create strings using single quotes, double quotes, and triple quotes
- Access individual characters using indexing
- Extract substrings using slicing
- Understand why strings are immutable
- Use basic string operators such as
+and*
Creating strings¶
A string is a sequence of characters used to represent text. In Python, you create a string by enclosing text in quotation marks. Python offers several ways to do this, and each has its own advantages.
Single quotes and double quotes¶
The most common way to create a string is with single quotes ('...') or double quotes ("..."). Both produce exactly the same result – Python treats them identically.
greeting = 'Hello, world!'
print(greeting)
farewell = "Goodbye, world!"
print(farewell)
One practical reason to have both options is that you can include one type of quote inside the other without any special handling.
# Use double quotes when the string contains an apostrophe
message = "It is a lovely day, isn't it?"
print(message)
# Use single quotes when the string contains double quotes
dialogue = 'She said, "Hello!"'
print(dialogue)
Triple quotes¶
For strings that span multiple lines, use triple quotes ('''...''' or """..."""). Triple-quoted strings preserve the line breaks exactly as you type them.
poem = """Roses are red,
Violets are blue,
Python is wonderful,
And strings are too."""
print(poem)
Escape characters¶
Sometimes you need to include special characters in a string that you cannot simply type. Python uses the backslash (\) as an escape character to represent these. The following are the most common escape sequences:
| Escape sequence | Meaning |
|---|---|
\n |
Newline (line break) |
\t |
Tab |
\\ |
Literal backslash |
\' |
Single quote |
\" |
Double quote |
# Using escape characters
print("First line\nSecond line")
print("Column1\tColumn2\tColumn3")
print("This is a backslash: \\")
Raw strings¶
If you need a string where backslashes are treated as literal characters (for example, when working with file paths), prefix the string with r to create a raw string.
# Without the r prefix, \n is interpreted as a newline
normal_string = "C:\new_folder\test"
print("Normal string:", normal_string)
# With the r prefix, backslashes are treated literally
raw_string = r"C:\new_folder\test"
print("Raw string: ", raw_string)
String length¶
To find out how many characters a string contains, use the built-in len() function. Every character counts, including spaces, punctuation, and escape sequences (each escape sequence counts as one character).
text = "Hello, world!"
print(len(text))
# Spaces count as characters
spaced = "a b c"
print(len(spaced))
# An empty string has length zero
empty = ""
print(len(empty))
Indexing¶
You can access individual characters in a string using indexing. Python uses zero-based indexing, which means the first character is at position 0, the second at position 1, and so on.
Use square brackets after the string (or variable name) with the index number inside: text[0].
word = "Python"
print(word[0]) # First character
print(word[1]) # Second character
print(word[5]) # Sixth (last) character
Negative indexing¶
Python also supports negative indexing, which counts from the end of the string. The last character is at index -1, the second-to-last at -2, and so on. This is very convenient when you need to access characters near the end of a string without knowing its exact length.
word = "Python"
print(word[-1]) # Last character
print(word[-2]) # Second-to-last character
print(word[-6]) # First character (same as word[0])
Index out of range¶
If you try to access an index that does not exist, Python raises an IndexError. The following cell demonstrates this – do not worry about the error, it is expected!
word = "Python"
try:
print(word[10])
except IndexError as error:
print(f"IndexError: {error}")
Slicing¶
Slicing allows you to extract a portion (a substring) of a string. The syntax is text[start:stop], where:
startis the index where the slice begins (inclusive)stopis the index where the slice ends (exclusive – the character at this position is not included)
Think of the indices as pointing between the characters, with 0 before the first character and len(text) after the last.
text = "Hello, world!"
print(text[0:5]) # Characters from index 0 up to (not including) index 5
print(text[7:12]) # Characters from index 7 up to (not including) index 12
Omitting start or stop¶
You can omit the start or stop value. When you omit start, the slice begins at the beginning of the string. When you omit stop, the slice continues to the end.
text = "Hello, world!"
print(text[:5]) # From the beginning up to index 5
print(text[7:]) # From index 7 to the end
print(text[:]) # A copy of the entire string
Slicing with a step¶
You can add a third value to the slice – the step -- using the syntax text[start:stop:step]. The step determines how many characters to skip between each selected character.
text = "abcdefghij"
print(text[0:10:2]) # Every second character
print(text[1:10:2]) # Every second character, starting from index 1
print(text[::3]) # Every third character from the whole string
Reversing a string¶
A particularly useful trick is to reverse a string using a step of -1.
text = "Python"
reversed_text = text[::-1]
print(reversed_text)
String immutability¶
An important characteristic of strings in Python is that they are immutable -- once a string is created, you cannot change its individual characters. If you try to assign a new character to a specific index, Python raises a TypeError.
word = "Hello"
try:
word[0] = "J"
except TypeError as error:
print(f"TypeError: {error}")
Instead of modifying a string in place, you create a new string with the changes you want. There are several ways to do this – here is one using concatenation and slicing.
word = "Hello"
# Create a new string with 'J' in place of the first character
new_word = "J" + word[1:]
print(new_word)
# The original string is unchanged
print(word)
Why are strings immutable? Immutability has several benefits:
- Safety: You can pass strings to functions without worrying that the function will change them unexpectedly.
- Efficiency: Python can optimise how it stores and reuses strings in memory.
- Hashing: Immutable objects can be used as dictionary keys and in sets, which require a stable hash value.
String operators¶
Python provides several operators that work with strings, making it easy to combine, repeat, and search text.
Concatenation with +¶
The + operator joins two strings together into a new string. This is called concatenation.
first_name = "Ada"
last_name = "Lovelace"
full_name = first_name + " " + last_name
print(full_name)
Repetition with *¶
The * operator repeats a string a given number of times.
line = "-" * 40
print(line)
echo = "ha" * 3
print(echo)
Membership testing with in and not in¶
You can check whether a substring exists within a string using the in and not in operators. These return True or False.
sentence = "The quick brown fox jumps over the lazy dog"
print("fox" in sentence)
print("cat" in sentence)
print("cat" not in sentence)
Exercises¶
Now it is time to practise what you have learned. Try each exercise in the empty code cell below it. If you get stuck, you can check the solutions at the end of this section.
Exercise 1¶
Create a variable called full_address that contains the following text on three separate lines (use a single string):
221B Baker Street
London
United Kingdom
Print the result.
# Exercise 1: Your code here
Exercise 2¶
Given the string "abcdefghijklmnopqrstuvwxyz", use slicing to extract:
- The first five letters
- The last five letters
- Every third letter
Print each result.
# Exercise 2: Your code here
alphabet = "abcdefghijklmnopqrstuvwxyz"
Exercise 3¶
Given the string "racecar", write code to check whether it is a palindrome (a word that reads the same forwards and backwards). Print True if it is, or False if it is not.
# Exercise 3: Your code here
word = "racecar"
Exercise 4¶
Given the string "Hello, World!", create a new string where the comma is replaced with a semicolon. Do this using concatenation and slicing (not the str.replace() method). Print the result.
# Exercise 4: Your code here
text = "Hello, World!"
Solutions¶
Expand the cells below to check your answers. It is perfectly fine if your solution differs from the one shown – there is often more than one correct approach.
# Solution 1
full_address = "221B Baker Street\nLondon\nUnited Kingdom"
print(full_address)
# Solution 2
alphabet = "abcdefghijklmnopqrstuvwxyz"
print(alphabet[:5]) # First five letters
print(alphabet[-5:]) # Last five letters
print(alphabet[::3]) # Every third letter
# Solution 3
word = "racecar"
print(word == word[::-1])
# Solution 4
text = "Hello, World!"
new_text = text[:5] + ";" + text[6:]
print(new_text)
Summary¶
Well done – you have covered the fundamentals of working with strings in Python! Here is a recap of the key points:
- Strings are sequences of characters, created with single quotes (
'...'), double quotes ("..."), or triple quotes ('''...'''/"""...""") - The
len()function returns the number of characters in a string - Indexing lets you access individual characters using
text[index], with zero-based positive indexing and negative indexing from the end - Slicing lets you extract substrings using
text[start:stop:step] - Strings are immutable -- you cannot change them in place, but you can create new strings based on existing ones
- The
+operator concatenates strings, the*operator repeats them, andin/not intest for membership
In the next tutorial, String methods, you will learn about the powerful built-in methods that Python provides for transforming, searching, and manipulating strings.