🔤 Learn how to work with strings in Python, including raw strings, escape characters, newline handling, and the use of triple quotes for multiline strings.
This section covers:
- How to handle special characters like backslashes
- The difference between regular and raw strings
- How to create multiline strings using triple quotes
- String immutability and how to modify strings
- Best practices for string formatting using f-strings
- 📜 How to use raw strings to avoid escaping backslashes
- 🧱 Create multiline strings using
'''or"""(triple single/double quotes) - 🔁 Understand that strings are immutable — you can't change them directly
- 🎞️ Use f-strings for dynamic string interpolation
- 🧩 Escape characters and newline behavior
- 💡 Hidden tips and best practices for working with strings in real-world applications
In Python, the backslash \ is an escape character. But sometimes you want to treat it literally — especially when working with file paths, regex patterns, or URLs.
🔹 Use Case: File paths on Windows, regex patterns, etc.
🔹 Syntax: Prefix your string with r
path = r'C:\new\folder'
print(path) # Output: C:\new\folder🔸 Without raw strings, \n would be interpreted as a newline:
path = 'C:\new\folder'
print(path)
# Output:
# C:
# ew\folderUse triple quotes (''' or """) to define strings that span multiple lines.
🔹 Example – Help message:
help_message = '''Usage: mysql command
-h hostname
-d database name
-u username
-p password'''
print(help_message)🔸 Triple-quoted strings preserve whitespace and line breaks exactly as written.
- Configuration blocks
- SQL queries
- Documentation inside code
- Templates
Once created, you cannot change a string’s contents directly.
🔹 Example – Attempting to Modify a Character:
s = "Python String"
s[0] = 'J' # ❌ Raises TypeError🔹 Correct Way – Create a New String:
s = "Python String"
new_s = 'J' + s[1:]
print(new_s) # Output: Jython String🔸 This applies to all string modification operations — always return a new string.
F-strings (formatted string literals) were introduced in Python 3.6 and are now the preferred way to format strings.
🔹 Basic Example:
name = 'Anthony'
message = f'Hello, {name}!'
print(message) # Output: Hello, Anthony!🔹 Evaluate Expressions Inline:
a = 5
b = 10
print(f'The sum is {a + b}') # Output: The sum is 15🔹 Debugging Aid – Self-documenting Expressions (Python 3.8+):
x = 10
print(f'{x=}') # Output: x=10| Issue | Explanation | Fix |
|---|---|---|
\n not showing |
You forgot to include newline manually | Add \n where needed |
' or " conflict |
Mixing quote types causes syntax error | Use different quotes or escape |
| Accidental path issues | Backslashes cause escape issues | Use raw strings r'' |
| Modifying strings fails | Strings are immutable | Reassign result to new variable |
| Sequence | Description |
|---|---|
\\ |
Backslash |
\' |
Single quote |
\" |
Double quote |
\n |
Newline |
\t |
Tab |
\r |
Carriage return |
\b |
Backspace |
🔹 Example – Using Escape Characters:
quote = "He said, \"Hello world!\""
print(quote) # Output: He said, "Hello world!"win_path = r'C:\Users\Name\AppData\Roaming'
print(win_path) # Output: C:\Users\Name\AppData\Roamingquery = """
SELECT id, name
FROM users
WHERE active = TRUE;
"""
print(query)product = "Laptop"
price = 999.99
print(f"Product: {product}, Price: ${price:.2f}")
# Output: Product: Laptop, Price: $999.99🧠 Hidden Tips & Notes
- 🧩 Use raw strings (
r"") whenever dealing with:- Windows file paths
- Regular expressions
- JSON or XML content
- 📝 Use triple quotes for:
- Long help messages
- Multi-line documentation
- Embedded scripts or templates
- 🔁 Since strings are immutable, any operation like slicing or concatenation returns a new string object.
- 🧵 Always use f-strings over
.format()or%formatting for better readability and performance. - 🧹 Be careful with escape sequences — if you don’t need them, prefer raw strings.
- 🧊 For debugging, use
repr()to see escape characters clearly:print(repr("Hello\nWorld")) # Output: 'Hello\nWorld'
| Feature | Syntax | Use Case |
|---|---|---|
| Raw string | r"..." |
Prevent escape interpretation |
| Multiline string | '''...''' or """...""" |
Span across multiple lines |
| Escape character | \n, \t, etc. |
Insert special characters |
| F-strings | f"{variable}" |
Interpolate values into strings |
| Immutability | ❌ No direct changes | Always create a new string |
| Debugging | repr(string) |
See actual escape characters |
🎉 Congratulations! You now have a solid understanding of how to work with Python strings, including how to handle special characters, write clean multiline text, and use modern formatting techniques like f-strings.
Next up: 🛠️ Section 15: Third-party Packages, PIP, and Virtual Environments – learn how to install packages, manage dependencies, and isolate environments.