Skip to Content
Course content

18: Strings: Creation and Basic Operations

Click on the "Edit" button in the top corner of the screen to edit your slide content.

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.
Rating
0 0

There are no comments for now.

to be the first to leave a comment.