8000 python-cheatsheet/Section_14_strings at main · onyxwizard/python-cheatsheet · GitHub
[go: up one dir, main page]

Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 

README.md

💬 Section 14: Strings

🔤 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

🧠 What You'll Learn

  • 📜 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

🧵 Raw Strings – Avoid Escaping Backslashes

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\folder

📄 Multiline Strings – Span Across Multiple Lines

Use 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.

✅ When to Use:

  • Configuration blocks
  • SQL queries
  • Documentation inside code
  • Templates

🚫 Strings Are Immutable

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 – Embed Variables Inside Strings

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

⚠️ Common Gotchas with Strings

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

🧩 Escape Characters & Special Sequences

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!"

🧪 Real-World Examples

✅ Example 1: Path Handling with Raw Strings

win_path = r'C:\Users\Name\AppData\Roaming'
print(win_path)  # Output: C:\Users\Name\AppData\Roaming

✅ Example 2: Multiline SQL Query

query = """
SELECT id, name
FROM users
WHERE active = TRUE;
"""
print(query)

✅ Example 3: F-string for Dynamic Messages

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'

📌 Summary

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.

0