Python Strings
f-strings
f"..." Allows you to embed Python expressions inside strings using {}.
f"..." is equivalent to str.format(), but more concise and easier to read.
name = "Alice"
age = 30
# f-string
message = f"My name is {name} and I'm {age} years old"
# Result: "My name is Alice and I'm 30 years old"
# Equivalent to str.format(), but more verbose
message = "My name is {} and I'm {} years old".format(name, age)
# Can include expressions
message = f"Next year I'll be {age + 1}"
# Result: "Next year I'll be 31"
# Can format strings
price = 19.9999999
message = f"Price: ${price:.2f}"
# Result: "Price: $20.00"
f-strings can be used together with = specifier to print both the expression and its value, which is very useful for debugging.
x = 10
print(f"{x=}") # Output: x=10
print(f"{x*2=}") # Output: x*2=20
To format numbers, use : followed by format specifiers. E.g., :.2f formats a float to 2 decimal places.
r-strings
r"..." Does NOT interpret escape sequences. E.g., treats backslashes \ as literal characters.
Raw strings are useful for file paths. This is especially helpful on Windows where backslashes are common in paths, though less critical on Unix/Mac since forward slashes donβt need escaping.
# Normal string - \n is interpreted as newline
path1 = "C:\new\test" # Problem! \n becomes newline, \t becomes tab
# Raw string - \n stays as \n
path2 = r"C:\new\test" # Correct! Keeps backslashes
# Result: "C:\new\test"
# Windows file paths
file_path = r"C:\Users\menghan\Desktop\file.csv"
# Regex patterns
pattern = r"\d{3}-\d{4}" # Matches phone numbers like 123-4567
rf"..." Combines raw and f-string features. You can include expressions and also treat backslashes as literal characters.
username = "Alice"
# rf or fr - raw f-string (both raw AND formatted)
path = rf"C:\Users\{username}\Desktop"
Triple-quoted strings
"""...""" or '''...''' allows you to create multi-line strings without needing \n.
This is useful for SQL queries which often span multiple lines for readability.
# SQL query with f-string and triple quotes
query = f"""
SELECT *
FROM table
WHERE date = '{my_date}'
"""
# Return results as a DataFrame
res = conn.execute(query).df()
Triple-quoted strings will preserve your newlines and indentation. You donβt need to use escape characters, e.g., \n, to create newlines.
>>> triple_quoted = """This is a triple-quoted string.
... It can span multiple lines.
... You can include 'single' and "double" quotes without any issues."""
>>> print(triple_quoted)
This is a triple-quoted string.
It can span multiple lines.
You can include 'single' and "double" quotes without any issues.
ref: