Skip to main content

Working with text

Now that you know what strings are, let’s learn how to manipulate them. Python gives you powerful tools to work with text.

String operations & Formatting

Concatenation & Repetition

You can join strings together using + or repeat them using *:

String Formatting

Python provides multiple ways to format strings. While f-strings are the modern standard, you will also see the older .format() method in older codebases.

String Indexing & Slicing

Indexing

Every character in a string has a number representing its position, called an index. Indexes start at 0 for the first character. You can also count backward from the end using negative indexes (starting at -1).
Accessing an index that doesn’t exist (e.g., text[10]) raises an IndexError.

Slicing

Slicing lets you extract a section of a string using the syntax [start:stop:step]. The character at start is included, but the character at stop is excluded.

Advanced Slicing (Mixed Boundaries & Steps)

You can mix positive/negative indexes and positive/negative steps together:

Slicing Edge Cases

Slicing is very forgiving compared to indexing:

String methods

Python strings come with many built-in methods - functions you can call directly on any string. These methods let you transform text, search for patterns, and clean up data. The best part? You can often guess what they do from their names - upper() makes text uppercase, replace() replaces text, and so on.

Changing case

Cleaning strings

Finding and replacing

Splitting and Joining

You can split a string into a list of strings, and join a list of strings back together.
Python has over 40 string methods! We’ve covered the most common ones here. For a complete list, check the official Python documentation.

Common mistakes

Practice & Exercises

To reinforce what you’ve learned in this section (Concatenation, Repetition, and String Methods), practice with these interactive notebooks:

Follow-Along Practice

Practice f-strings, repetitions, case transformations, stripping whitespace, and replacing text.💻 VS Code | 🚀 Colab | 📥 Download

Practice Exercises

Test your knowledge with hands-on exercises on custom greetings, cleaning email addresses, and replacement logic.💻 VS Code | 🚀 Colab | 📥 Download

What’s next?

You’ve mastered working with data! Ready to make your programs smart with decision-making?

Control flow

Learn if statements and loops