String Handling
Working with text
Introduction
In this lesson, you'll learn about string handling in Python. Coming from C, you already have a foundation for understanding this concept. We'll build on that knowledge while highlighting the key differences.
In C, you're familiar with working with text.
Python has its own approach to working with text, which we'll explore step by step.
The Python Way
Let's see how Python handles this concept. Here's a typical example:
name = "Alice"
length = len(name) # 5
# Concatenation — f-strings are cleanest
greeting = f"Hello, {name}!"
# Comparison — use ==
if name == "Alice":
print("equal")
# Substring — slicing
sub = name[1:4] # "lic"
# Built-in methods
upper = name.upper() # "ALICE"
lower = name.lower() # "alice"
stripped = " hi ".strip() # "hi"
parts = "a,b,c".split(",") # ["a", "b", "c"]
joined = ",".join(["a","b"]) # "a,b"
found = name.find("lic") # 1
replaced = name.replace("l", "L") # "ALice"Comparing to C
Here's how you might have written similar code in C:
#include <stdio.h>
#include <string.h>
char name[] = "Alice";
int len = strlen(name); /* 5 */
/* Concatenation requires buffer */
char greeting[100];
snprintf(greeting, sizeof(greeting), "Hello, %s!", name);
/* Comparison */
if (strcmp(name, "Alice") == 0) { /* equal */ }
/* Substring — manual */
char sub[4];
strncpy(sub, name + 1, 3);
sub[3] = '\0';
/* Convert to uppercase — manual */
for (int i = 0; name[i]; i++) {
name[i] = toupper(name[i]);
}You may be used to different syntax or behavior.
C strings are null-terminated char arrays; Python strings are immutable objects
You may be used to different syntax or behavior.
C needs strlen; Python uses len()
You may be used to different syntax or behavior.
C string ops need explicit buffers; Python creates new strings automatically
You may be used to different syntax or behavior.
Python has dozens of built-in string methods; C has minimal string.h
Step-by-Step Breakdown
1. Strings Are Immutable Objects
Python strings are objects with methods. You never worry about buffer sizes or null terminators.
char s[100]; strncpy(s, "hello", sizeof(s));s = "hello" # just assign2. Rich String Methods
Python strings have over 40 built-in methods for searching, splitting, transforming, and checking content.
"hello world".title() # "Hello World"
"abc".startswith("ab") # True
" hi ".strip() # "hi"
"a,b,c".split(",") # ["a","b","c"]3. F-Strings
Python f-strings replace printf's complex format specifiers with readable inline expressions.
printf("%.2f%%\n", ratio * 100);print(f"{ratio * 100:.2f}%")Common Mistakes
When coming from C, developers often make these mistakes:
- C strings are null-terminated char arrays; Python strings are immutable objects
- C needs strlen; Python uses len()
- C string ops need explicit buffers; Python creates new strings automatically
Key Takeaways
- C null-terminated char arrays → Python immutable string objects
- strlen → len(), strcmp → ==
- snprintf format strings → f-strings
- Python has rich built-in string methods