Python Strings

In Python, strings are sequences of characters that can be manipulated and processed using a wide variety of string methods, including slicing, concatenation, formatting, and searching.
  • The built-in class is str
    • string is legacy and should not be used
  • Can be enclosed by single or double quotes but single is preferred
  • Backslash escapes allow you to include special characters in strings without causing syntax errors
  • A string can span multiple lines but each line must be escpaed by the backslash
  • A string enclosed within a pair of triple quotes """ can span multiple lines without the backslash escape
  • Strings are immutable; they cannot be changed after they’re created
  • Strings are processed with operators to create new alternate versions

A backslash example allows the output to include double quotes

print("She said, \"Hello!\"") # Output: She said, "Hello!"

Slice syntax

Slice syntax is a way to extract a subset of elements from a sequence, such as a string, list, or tuple.

Example of slice syntax

my_string = "Hello, world!"
my_slice = my_string[7:12]  # Extracts "world" from the string
print(my_slice)

We create a string named my_string and use slice syntax to extract the characters from index 7 (the first “w”) up to, but not including, index 12 (the “d”). The resulting slice is assigned to my_slice, which we then print to the console.

len()

The len() function takes a single argument, which should be a sequence, and returns the number of elements in the sequence. White space and all characters are included.

my_string = "Hello, world!"
string_length = len(my_string)
print(string_length)  # Output: 13

[]

[] is an important syntax in Python for creating, accessing, and manipulating lists.

A basic list with three integers (an integer is a whole number; a number with decimal is floating-point)

my_list = [1, 2, 3]
print(my_list)  # Output: [1, 2, 3]

A specific value from the list be retireived using zero based indexing (where the list item starts at 0 instead of 1).

print(my_list[0])  # Output: 1

+=

The += operator is a shorthand way of incrementing a variable by a certain amount.

x = 1
x += 1  # Equivalent to x = x + 1
print(x)  # Output: 2

In this example, we define a variable x with an initial value of 1. We then use the += operator to increment x by 1, which is equivalent to using the ++ operator in other programming languages. We then print the resulting value of x to the console, which outputs 2.

String methods

A string method is a built-in function that can be used to manipulate and process strings. String methods are applied to a string object, and they typically return a new string object with the result of the operation.

Example string methods

my_string = "hello, world!"

# Convert the first letter of the string to uppercase
new_string = my_string.capitalize()

# Replace all occurrences of the substring "o" with the substring "e"
new_string = my_string.replace("o", "e")

# Convert all letters in the string to uppercase
new_string = my_string.upper()

# Split the string into a list of words based on whitespace
word_list = my_string.split()

# Check if the string starts with the substring "hello"
starts_with_hello = my_string.startswith("hello")

Formatted string literal (f-strings)

A formatted string literal (also known as an f-string) allows you to embed expressions inside string literals, which are evaluated at runtime and inserted into the resulting string.

Formatted string literals are created by prefixing a string literal with the letter f or F. Inside the string, you can include expressions inside curly braces {} that will be replaced with their values at runtime.

Example code

name = "Alice"
age = 25
height = 1.68
print(f"My name is {name}, I am {age} years old, and I am {height:.2f} meters tall.")

In this example, we define three variables name, age, and height. We then use an f-string to create a formatted string that includes these variables. Inside the string, we include expressions inside curly braces {} that will be evaluated at runtime and inserted into the resulting string. The :.2f after height is a format specifier that formats the height variable as a floating-point with two decimal places.

Example result

My name is Alice, I am 25 years old, and I am 1.68 meters tall.

Unicode .v. Bytes

Unicode is a character encoding standard that assigns unique numerical codes (known as code points) to every character, symbol, and script used in written languages around the world.

In Python, strings are encoded using Unicode by default. This means that you can use any character from any language in a Python string, and it will be represented correctly as a Unicode code point. You can also encode and decode strings using different character encodings, such as ASCII, UTF-8, and ISO-8859-1, using built-in functions and libraries in Python.

Bytes are a built-in data type that represents a sequence of bytes, which are typically used to represent binary data such as images, audio files, and network packets.

A byte is a unit of digital information that consists of eight bits. Each bit can be either 0 or 1, which allows for 256 possible combinations of 0s and 1s in a single byte.

In Python, you can create a bytes object by using the bytes() constructor, which takes a sequence of integers (between 0 and 255) as its argument.

my_bytes = bytes([65, 66, 67])  # Creates a bytes object with the values 65, 66, and 67 (which represent the ASCII codes for A, B, and C)

If Statement

An if statement is a control flow statement that allows you to execute a block of code if a certain condition is true.

Basic syntax

if condition:
    # code to execute if the condition is true

In this syntax, the if keyword is followed by a condition that is enclosed in parentheses. If the condition evaluates to True, the code block that follows the if statement (indented using four spaces or a tab) will be executed. If the condition evaluates to False, the code block will be skipped.

You can also include an else block to execute a different block of code if the condition is false:

if condition1:
    # code to execute if condition1 is true
elif condition2:
    # code to execute if condition2 is true and condition1 is false
elif condition3:
    # code to execute if condition3 is true and conditions 1 and 2 are false
else:
    # code to execute if all conditions are false
Last modified July 21, 2024: update (e2ae86c)