C
PY

C to Python

10 lessons

Progress0%
1Variables & Types2Functions3Arrays → Lists4Structs → Classes & Dicts5Memory Management6String Handling7File I/O8Object-Oriented Programming9Exceptions and Context Managers10Standard Library and Tools
All Mirror Courses
C
PY
String Handling
MirrorLesson 6 of 10
Lesson 6

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.

Mirror Card
C
From C:

In C, you're familiar with working with text.

PY
In Python:

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:

PY
Python 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:

C
C (What you know)
#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]);
}
Mirror Card
C
From C:

You may be used to different syntax or behavior.

PY
In Python:

C strings are null-terminated char arrays; Python strings are immutable objects

Mirror Card
C
From C:

You may be used to different syntax or behavior.

PY
In Python:

C needs strlen; Python uses len()

Mirror Card
C
From C:

You may be used to different syntax or behavior.

PY
In Python:

C string ops need explicit buffers; Python creates new strings automatically

Mirror Card
C
From C:

You may be used to different syntax or behavior.

PY
In Python:

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.

C
C
char s[100]; strncpy(s, "hello", sizeof(s));
PY
Python
s = "hello"  # just assign

2. Rich String Methods

Python strings have over 40 built-in methods for searching, splitting, transforming, and checking content.

PY
Python
"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.

C
C
printf("%.2f%%\n", ratio * 100);
PY
Python
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
Common Pitfall
Don't assume Python works exactly like C. While the concepts may be similar, the syntax and behavior can differ significantly.

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
Rule of Thumb
The best way to learn is by doing. Try rewriting some of your C code in Python to practice these concepts.
PreviousNext