Lecture 3 — Strings and Files#
🎯 Learning Objectives
By the end of this lecture, you will be able to:
- ✅ Manipulate strings using indexing, slicing, and methods
- ✅ Format strings with f-strings and
.format() - ✅ Open, read, and write text files using
open()andwith - ✅ Navigate the filesystem with
osandpathlib - ✅ Handle different file modes: read, write, append, binary
📑 Table of Contents
🔗 Building on What You Know
You now know how to create variables of different types and perform operations on them. One of the most common types you'll work with is text — from user names to file paths to entire documents. This lecture focuses on mastering strings and connecting your programs to the outside world through files.
|
Instructor Yaé Gaba |
Course Python for Scientific Computing and Data Science |
Date October 2025 |
👨🏫 Instructor
- 📝 Yaé Ulrich Gaba: github.com/gabayae
🧾 Summary
This lecture aims to present strings, reviewing some of the most commonly encountered operations, particularly in text processing tasks. We will also examine the string class in greater detail, including how to use the open() function to open, read, and write to files.
🔑 Key Concepts
- 📝 String Operations: Common methods and text processing techniques
- 🔍 String Class: In-depth examination of string methods and properties
- 📁 File Handling: Using
open()to read and write files - 🔄 Text Processing: Practical applications and common patterns
🔗 Resources
- 📘 GitHub Repository: PyPro-SCiDaS — The Shell
- 💡 Feel free to explore, fork, and practice before the session!
📚 0. Introduction
Strings in Python are not merely standalone objects; rather, they are sequences of characters that represent text data. Similar to numerical data types, which consist of sequences of digits, strings consist of sequences of characters. These sequences are fundamental in Python programming, as they are often used to construct and manipulate more complex data structures like lists, tuples, and dictionaries.
Python treats strings as immutable sequences, meaning that once a string is created, its contents cannot be changed. However, various operations can be performed on strings to generate new strings or extract specific information. These operations include concatenation, slicing, formatting, and various methods for searching and transforming the text. Understanding how to effectively manipulate strings is crucial, as strings are one of the most commonly used data types in Python, especially in tasks involving data processing, file handling, and user input.
Moreover, strings serve as the foundation for more advanced objects and data structures. For instance, strings can be used to represent elements within lists or tuples, and they play a critical role in tasks ranging from basic text processing to complex data analysis. As we delve deeper into Python, we will explore the extensive functionality provided by string operations and how they integrate with other Python data types.
In addition to handling strings, Python provides robust support for working with files, which is essential for tasks like reading input data, storing output, or simply managing data over the course of a program's execution. Python uses the open() function to handle files, allowing you to open, read, write, and close them efficiently.
The open() function is versatile, enabling you to open a file in various modes, such as reading ('r'), writing ('w'), appending ('a'), or even reading and writing in binary mode ('rb', 'wb'). Once a file is opened, you can perform operations like reading the entire file content into a string, reading it line by line, or writing data back into the file. After completing these operations, it is important to close the file using the close() method to free up system resources and ensure data integrity.
Understanding how to work with files in Python is critical, especially when dealing with real-world data that often needs to be read from or written to external files. This capability, combined with string manipulation techniques, forms the backbone of many Python programs, enabling efficient data processing, storage, and retrieval.
📝 1. Strings
1.0. Definition of a string variable
A string variable is defined in the traditional manner by direct assignment using the equality symbol (as discussed in the section on variable creation). The only distinction between string variables and numerical variables is that the values of string variables must be enclosed in quotation marks during assignment. The three examples below illustrate this.
ch1 = 'Will you be at the meeting tonight?'
ch2 = '"Yes," he replies.'
ch3 = "Alright, I would appreciate it."
ch4 = """This sentence is a long string containing all types of quotes: " ' « » as well as many special characters."""
print(ch1)
print("================================================")
print('\t')
print(ch2)
print("================================================")
print('\t')
print(ch3)
print("================================================")
print('\t')
print(ch4)
💬 String Variable Examples
These four examples demonstrate different uses of quotation marks when defining a string variable.
- In the first example (
ch1), single quotes are used because there is no issue with the string specified. Double quotes could also be used. - In the second example (
ch2), single quotes are used because the specified string already contains double quotes as values. - In the third example (
ch3), double quotes are used because the text contains apostrophes, which are actually single quotes. - In the fourth example (
ch4), triple quotes are used because the text contains not only single quotes, apostrophes, and double quotes but also spans multiple lines. In this case, triple quotes are necessary.
🔤 1.1. Indexing strings and slicing
A string is a sequence of ordered and indexed values. This means you can access each element in the sequence by specifying its index, similar to lists. Portions of a string can also be extracted using slices, with the general notation [start=0]:[stop=len][:step=1]. Indexing in Python starts at 0: the 1st element of a list is at index 0, the 2nd is at index 1, and so on. Thus, the n elements of a list are indexed from 0 to n-1. The examples below illustrate this.
Indexing:
To access a specific character in a string, you use the index inside square brackets. For example:
ch[0]returns the first character of the stringch.ch[2]returns the third character.ch[-1]returns the last character of the string.
Slicing:
Slicing allows you to extract a portion of the string. The syntax for slicing is [start:stop:step], where:
startis the index of the first character you want to include in the slice.stopis the index of the character just after the last character you want to include.stepspecifies the interval between characters in the slice (optional; default is 1).
Examples of slicing:
ch[:6]returns a substring from the beginning ofchup to, but not including, index 6.ch[6:]returns a substring from index 6 to the end of the string.ch[0:10:2]returns every second character from index 0 to 10.
Examples:
print(ch[0]) # Returns 'C'
print(ch[2]) # Returns 'r'
print(ch[-1]) # Returns 'e', the last character
print(ch[:6]) # Returns 'Christ'
print(ch[6:]) # Returns 'elle'
print(ch[0:10:2]) # Returns 'Crsel'
These operations make it easy to manipulate and extract data from strings, which is crucial for text processing and data manipulation in Python.
ch="Christelle"
print(ch[0]) # returns 'C'
print(ch[2]) # returns 'r'
print(ch[-1]) # returns 'e', the last element of ch
print(ch[:6]) # returns 'Christ'
print(ch[6 :]) # returns 'elle'
print(ch[0:10:2]) # returns 'Crsel'
The elements of a string are not defined by words separated by spaces but by the characters that make up the string. In fact, even if the string consists of a sentence with multiple words, indexing is done solely based on the individual characters that form the entire string.
💡 Important Note: You can manipulate a string (and generally any sequence) using functions (procedural concept) or methods (object-oriented concept). We will discuss this further below.
📏 1.2. Length of a string
To determine the length of a string, the len() function is used.
print(len(ch)) # returns 37; The string 'ch' consists of 37 characters, including spaces.
ch = "This is a string of several words"
len(ch) # returns 36; The string 'ch' consists of 36 characters, including spaces.
37
🔗 1.3. Addition of strings (Concatenation)
String addition refers to the process of placing strings together, one after the other, to form a single string. This operation is known as concatenation. To perform concatenation, the + symbol is used between the string variable names. The resulting string is then assigned to a new variable. Examples:
str2 = "World"
result = str1 + " " + str2 # Concatenates 'Hello' and 'World' with a space in between
print(result) # Outputs: Hello World
x = "One small step for man,"
y = "one giant leap for mankind."
z = x + " " + y # Concatenates the two strings with a space in between
z # Outputs: 'Un petit pas pour l'homme, un grand pas pour l'humanité'
"Un petit pas pour l'homme, un grand pas pour l'humanité."
⚠️ Concatenation with Numeric Values
Be cautious when using the concatenation operator to combine a numeric value with a string to form a single string. For example:
y = 5
z = 'dollars'
If you want to concatenate x, y, and z to obtain the sentence 'The price of the pen is 5 dollars', you can't directly do x + y + z because y is a numeric type. You need to first convert y to a string using the str() function. Here's how it should be done:
print(result) # Outputs: 'The price of the pen is 5 dollars'
x = 'The price of the pen is'
y = 5
z = 'dollars'
ch = x + " " + " " + str(y) + " " + z
ch # returns 'The price of the pen is5euros'
'Le prix du stylo est 5 euros'
🔧 1.4. Methods
Strings come with numerous features—referred to as methods in Object-Oriented Programming (OOP)—that facilitate their manipulation. The dir() function lists all available methods for strings. Among these, the most commonly used are upper(), lower(), capitalize(), find(), replace(), count(), startswith(), endswith(), and the in keyword.
These methods allow for operations such as converting text to different cases, searching for and replacing substrings, and checking string content for specific patterns. They are crucial for text processing and data manipulation tasks, enabling efficient handling and transformation of string data. Understanding and utilizing these methods effectively can greatly enhance the capability to work with and analyze text-based information in Python.
title = "Python course"
help(title.replace)
enfant, peluche = "Calvin", 'Hobbes' # Multiple assignment
titre = enfant + ' et ' + peluche # +: String concatenation
print(titre)
print("================================================")
print('\t')
print(titre.replace('et', '&')) # Substring replacement: 'Calvin & Hobbes'
print("================================================")
print('\t')
print(' & '.join(titre.split(' et '))) # Splitting and joining
print("================================================")
print('\t')
print('Hobbes' in titre) # in: Inclusion test
print("================================================")
print('\t')
print(titre.find("Hobbes")) # str.find: Substring search
print(titre.center(30, '-')) # '-------Calvin et Hobbes-------'
dir(str) # Lists all string methods
🎨 1.5. Formatting
It is often necessary to retrieve the value of a variable and incorporate it into a string to form a new, usable string for other purposes. For instance, consider a variable prix defined as follows:
We want to display a message like "The price of the pen is 2 euros".
The goal of formatting is to embed the value of a variable within a string. There are three primary methods to achieve this:
- Using the concatenation operator
+ - Using the formatting operator
% - Using the
str.format()method
We will illustrate just one example using the str.format() method. The formatting system allows for precise control over the conversion of variables into strings. It primarily relies on the str.format() method, which enables detailed specification of how variables should be inserted and formatted within a string. For more information, you can refer to the formatting documentation.
print("{nom} a {age} ans".format(nom='Calvin', age=6)) # 'Calvin a 6 ans'
print("================================================")
print('\t')
print("{} a {} ans".format('Calvin', 6)) # Shortcut 'Calvin a 6 ans'
print("================================================")
print('\t')
pi = 3.1415926535897931
print("{x:f} {x:.2f} {y:f} {y:g}".format(x=pi, y=pi*1e9)) # '3.141593 3.14 3141592653.589793 3.14159e+09'
📋 Explanation
- Named Fields: In the first example,
"{nom} a {age} ans".format(nom='Calvin', age=6)uses named fields in the format string. The placeholders{nom}and{age}are replaced by the values provided in theformatmethod. - Positional Fields: The second example,
"{} a {} ans".format('Calvin', 6), uses positional placeholders{}. The values are inserted in the order they appear in theformatmethod. - Formatting Numbers: The third example demonstrates formatting numerical values.
"{x:f} {x:.2f} {y:f} {y:g}".format(x=pi, y=pi*1e9)formats the floating-point numberpiin various ways:{x:f}displayspiwith six decimal places.{x:.2f}formatspito two decimal places.{y:f}showspi*1e9with six decimal places.{y:g}formatspi*1e9in a more compact form using scientific notation.
print("Calvin and Hobbes\nScientific progress goes 'boink'") # Calvin and Hobbes Scientific progress goes 'boink'
print("{0} fois {1} font {2}".format(3, 4, 3*4)) # Formatting and display
# Output: 3 fois 4 font 12
# A small exercise in this section is to display a multiplication table.
📋 Explanation
- Indexed Fields: In the example
"{0} fois {1} font {2}".format(3, 4, 3*4), the{0},{1}, and{2}are positional placeholders. They are replaced by the values3,4, and3*4(which evaluates to12) respectively. This results in the output:3 fois 4 font 12. - Multiplication Table Exercise: The comment suggests a small exercise where one can display a multiplication table, which would involve iterating through numbers and formatting them into a table structure using similar formatting techniques.
In concluding this section, we mention the study of regular expressions (regex or re), which will not be discussed in this course. As a guide, we can say that regular expressions are extensions of the str() function that allow for more complex pattern-matching operations than what the str() functions alone can achieve.
Practical
Explore the functions:
💡 Note
It is important to distinguish between a character value and an alphabetical value. An alphabetical value is always a string, whereas a string is not necessarily an alphabetical value. In Python, a character value is always enclosed in quotes (single, double, or triple), while a numeric value is expressed without quotes. There are two main types of data: numeric data and character data. It is worth noting that a numeric value enclosed in quotes is automatically treated as a character value, even if it is not alphabetical. Keeping these details in mind is crucial when handling sequences of values in Python (see the example below):
y = "12" # y is a string formed by digits
z = "mon texte" # z is a string formed by alphabetical values
k = "Ce stylo coûte 5 euros" # k is a string formed by alphanumeric values
For more details on string processing, refer to this page.
📁 2. File I/O
File I/O (Input/Output) in Python involves reading from and writing to files. Python provides several built-in functions and methods to handle files. The primary function for file manipulation is open(), which allows you to open a file and returns a file object. This file object provides methods for reading and writing data. Here's a detailed overview with various examples:
Basic File Operations
1. Opening and Closing a File
To open a file, use the open() function. The basic syntax is open(filename, mode), where filename is the name of the file and mode is the mode of operation ('r' for read, 'w' for write, 'a' for append, etc.). To close a file, use the close() method of the file object.
file = open('example.txt', 'r')
# Perform operations on the file
file.close()
2. Reading from a File
read() method reads the entire content of the file. readline() method reads a single line from the file, and readlines() method reads all lines into a list.
file = open('example.txt', 'r')
content = file.read()
print(content)
file.close()
# Read line by line
file = open('example.txt', 'r')
line = file.readline()
while line:
print(line, end='')
line = file.readline()
file.close()
# Read all lines into a list
file = open('example.txt', 'r')
lines = file.readlines()
print(lines)
file.close()
3. Writing to a File
write() method writes a string to the file. writelines() method writes a list of strings to the file.
file = open('example.txt', 'w')
file.write('Hello, World!')
file.close()
# Write multiple lines to a file
lines = ['First line\n', 'Second line\n', 'Third line\n']
file = open('example.txt', 'w')
file.writelines(lines)
file.close()
4. Appending to a File
To append data to an existing file, open the file in append mode ('a').
file.write('This is an appended line.\n')
file.close()
5. Using with Statement
The with statement is used for resource management. It ensures that the file is properly closed after its suite finishes, even if an exception is raised.
with open('example.txt', 'r') as file:
content = file.read()
print(content)
# Writing to a file using with statement
with open('example.txt', 'w') as file:
file.write('This is a new content.')
6. File Modes
'r': Read (default mode, opens the file for reading)
'w': Write (creates a new file or truncates an existing file)
'a': Append (opens the file for appending)
'b': Binary (reads or writes in binary mode, e.g., 'rb' or 'wb')
't': Text (default mode, opens the file in text mode, e.g., 'rt' or 'wt')
with open('example.jpg', 'rb') as file:
content = file.read()
# Process binary data
7. File Positioning
seek(offset, whence): Moves the file pointer to a specific position.
tell(): Returns the current file pointer position.
file.seek(10) # Move to the 10th byte
print(file.tell()) # Print the current file pointer position
content = file.read(20) # Read 20 bytes from the current position
print(content)
8. Working with File Paths
Use the os and pathlib modules to handle file paths.
from pathlib import Path
# Get current working directory
print(os.getcwd())
# Join paths
file_path = os.path.join('folder', 'example.txt')
# Using pathlib
path = Path('folder') / 'example.txt'
9. Handling File Exceptions
Use try and except blocks to handle file-related exceptions.
with open('example.txt', 'r') as file:
content = file.read()
except FileNotFoundError:
print('File not found.')
except IOError:
print('An IOError occurred.')
These examples cover a wide range of file operations in Python. Whether you are reading from, writing to, or managing files, Python's file I/O capabilities provide a flexible and powerful way to handle file data.
ℹ️ Help Information for open()
The help information for open is below:
# Get help documentation
help(open)
On your machine, in the folder notebooks_lecture_notes, create a file name testfile.txt and populate it withthis info:
fghij
aims class
Rwanda 2025
this is really cool
In the code below, I’ve opened that file:
$ cat testfile.txt
fghij
aims class
Rwanda 2025
this is really cool
Now let’s open this file in Python:
f = open('testfile.txt','r')
s = f.read(3)
print(s)
fgh
We read the first three characters, where each character is a byte long.
We can see that the file handle points to the 4th byte (index number 3) in the file:
f.tell()
3
f.read(1)
'i'
f.close() # close the old handle
f.read() # can't read anymore because the file is closed.
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[7], line 1
----> 1 f.read()
ValueError: I/O operation on closed file.
The file we are using is a long series of characters, but two of the characters are new line characters. If we looked at the file in sequence, it would look like "abcdenfghijn". Separating a file into lines is popular enough that there are two ways to read whole lines in a file. The first is to use the readlines() method:
f = open('testfile.txt','r')
lines = f.readlines()
print(lines)
f.close() # Always close the file when you are done with it
['fghij\n', 'aims class\n', 'Rwanda 2025\n', 'this is really cool']
A very important point about the readline method is that it keeps the newline character at the end of each line. You can use the strip() method to get rid of the string.
File handles are also iterable, which means we can use them in for loops or list extensions:
f = open('testfile.txt','r')
lines = [line.strip() for line in f]
f.close()
print(lines)
['fghij', 'aims class', 'Rwanda 2025', 'this is really cool']
lines = []
f = open('testfile.txt','r')
for line in f:
lines.append(line.strip())
f.close()
print(lines)
These are equivalent operations. It's often best to handle a file one line at a time, particularly when the file is so large it might not fit in memory.
The other half of the story is writing output to files. We'll talk about two techniques: writing to the shell and writing to files directly.
If your program only creates one stream of output, it's often a good idea to write to the shell using the print function. There are several advantages to this strategy, including the fact that it allows the user to select where they want to store the output without worrying about any command line flags. You can use \> to direct the output of your program to a file or use | to pipe it to another program.
Sometimes, you need to direct your output directly to a file handle. For instance, if your program produces two output streams, you may want to assign two open file handles. Opening a file for reading simply requires changing the second option from r to w or a.
⚠️ Caution! Opening a file with the 'w' option means start writing at the beginning, which may overwrite old material. If you want to append to the file without losing what is already there, open it with 'a'.
Writing to a file uses the write() command, which accepts a string.
outfile = open('outfile.txt','w')
outfile.write('This is the first line!')
outfile.close()
Another way to write to a file is to use writelines(), which accepts a list of strings and writes them in order.
⚠️ Caution! writelines() does not append newlines. If you really want to write a newline at the end of each string in the list, add it yourself.
🎯 Key Takeaways
- Strings are immutable sequences of characters, created with single or double quotes.
- Indexing (
s[0]) and slicing (s[1:4]) let you access parts of a string. - String methods like
.upper(),.split(),.replace(),.format()are essential tools. - f-strings (
f"Hello {name}") provide the most readable way to format strings. - File I/O uses
open()with modesr,w,a— always usewithstatements for safe handling.
—
—