Python
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax and Data Types
-
Section 3: Collections
-
39: Set Operations: Union, Intersection, Difference
-
Section 4: Control Flow
-
Section 5: Functions
-
Section 6: Turtle Graphics and Early Practice Projects
-
Section 7: Working with Files and I/O
-
Section 8: Regular Expressions
-
Section 9: Object-Oriented Python
-
Section 10: Error Handling
-
Section 11: Modules and Packages
-
Section 12: Iterators, Generators, and Functional Tools
-
Section 13: Decorators and Metaprogramming
-
Section 14: Concurrency and Parallelism
-
Section 15: Working with Dates, Times, and Numbers
-
Section 16: Standard Library Deep Dive I: Data Structures
-
Section 17: Standard Library Deep Dive II: System and Introspection
-
Section 18: Standard Library Deep Dive III: Security and Encoding
-
Section 19: Standard Library Deep Dive IV: Text and Data Utilities
-
Section 20: Networking and Web Basics
-
Section 21: Working with Databases
-
Section 22: Testing and Quality
-
Section 23: Advanced Typing
-
Section 24: Context Managers and Resource Handling
-
Section 25: Text, Unicode, and Binary Data
-
Section 26: More Functional and Iteration Tools
-
Section 27: Data Validation and Configuration
-
Section 28: Working with Images and Media
-
Section 29: Property-Based and Documentation Testing
-
Section 30: Packaging and Deployment
-
Section 31: Performance and Internals
-
Section 32: Design Patterns in Python
-
Section 33: GUI Programming
-
Section 34: Security Basics
-
Section 35: Data Structures and Algorithms
-
Section 36: Practical Projects
-
Section 37: Capstone Projects
-
Section 38: Interview and Algorithm Practice
-
Section 39: Writing Idiomatic Python
18: Strings: Creation and Basic Operations
I've found that the best way to get a feel for strings in Python isn't by reading a list of definitions, but by actually trying to build something and seeing where the language pushes back. Let's imagine we're building a simple system to handle product labels for a warehouse. I'll start by trying to define a product name and see what happens.
The Apostrophe Headache
# Let's try to set up a product name
product_name = 'Collector's Edition Vinyl'
print(product_name)
Right away, Python throws a SyntaxError. It's because I started the string with a single quote, and when it hit the apostrophe in "Collector's", it thought the string had ended. It has no idea what to do with the rest of the characters. I could use a backslash to "escape" that quote, but that gets messy fast. Instead, I'll just swap to double quotes.
product_name = "Collector's Edition Vinyl"
print(product_name) # Now it works perfectly.
The rule of thumb I use is simple: if your text contains single quotes, wrap it in double quotes. If it contains double quotes, wrap it in single quotes. Python doesn't actually care which one you use as long as they match.
Handling Long Descriptions
Now, a product name is short, but what about a full product description? I want it to span a few lines so it's readable in the code, but I don't want to manually type \n at the end of every line.
# This won't work
description = "This is a high-quality
vinyl record
pressed in 180g wax."
Python hates that. It expects a string to stay on one line unless you tell it otherwise. To fix this, I'll use triple quotes. This is a bit of a "super-string" that preserves every line break and quote inside it exactly as I type it.
description = """This is a high-quality
vinyl record
pressed in 180g wax."""
print(description)
The "Type" Clash
Let's say we have a product ID number that's an integer, and I want to combine it with the name to create a unique label. My first instinct is to just "add" them together.
product_id = 5021
label = "Item: " + product_name + " ID: " + product_id
print(label)
And there it is: a TypeError. Python is strictly typed here; it refuses to add a string to an integer because that doesn't make logical sense to the interpreter. To get around this, I have to explicitly cast the number into a string using the str() function.
label = "Item: " + product_name + " ID: " + str(product_id)
print(label)
It's a bit clunky, but it's the fundamental way concatenation works. You're essentially gluing pieces of text together.
Slicing into the Data
Finally, let's look at how to pull specific pieces of data out of a string. Imagine our warehouse uses a standardized code like "VINYL-2023-BLUE". I only want the year part.
sku = "VINYL-2023-BLUE"
# I know 'VINYL-' is 6 characters, so I'll start at index 6
year = sku[6:10]
print(year) # Output: 2023
But wait, what if the prefix isn't always 6 characters? If I want the color at the end, I don't want to have to count the characters from the beginning every time. I'll use negative indexing instead. Negative numbers count backward from the end of the string.
# Let's grab the last 4 characters
color = sku[-4:]
print(color) # Output: BLUE
Slicing is incredibly powerful. By using [start:stop], I can carve out exactly what I need without having to rewrite the string.
📋 Practical Task
Exercise: The Warehouse SKU Parser
You've been handed a raw data string from an old inventory system. The string follows a strict format: "CATEGORY_ITEMID_LOCATION" (e.g., "ELECTRONICS_9921_WAREHOUSE_B").
Write a script that does the following:
- Assign the string
"FURNITURE_4402_WAREHOUSE_A"to a variable. - Use string slicing to extract the category (the word before the first underscore).
- Use string slicing to extract the location (the word after the final underscore).
- Create a new string that combines these two pieces of information into a human-readable sentence, such as:
"The FURNITURE item is located in WAREHOUSE_A." - Print the final sentence to the console.
There are no comments for now.