Every program works with text. A login form reads a name, a report prints a title, a web scraper pulls headlines from a page, and a chatbot reads what you type. In Python, all of that text lives in one data type: the string. You will use strings in almost every program you ever write, so it pays to learn them properly.
A string is a sequence of characters, such as letters, digits, spaces and symbols, written inside quotes. Python gives you a rich set of tools for strings: you can join them, cut pieces out, search them, change their case, clean them and fill them with values. One rule surprises every beginner: a string cannot be changed after it is created. We will see what that means and how to work with it.
In this tutorial, we start with how to write strings and use quotes, then cover multiline strings, joining strings, indexing and slicing. We go through the most useful string methods, with every example run and its real output shown, including f strings, join and the difference between find and index. We end with common mistakes and the full program. The outputs on this page come from a real run on Python 3.13.
A string is an ordered sequence of characters. You create one by putting text inside quotes. Each character has a position, called an index, counted from 0 at the left. You can also count from the right with negative numbers, where minus 1 is the last character.
A string literal is text written directly in your code. Python accepts single quotes, double quotes and triple quotes. The first three lines below print the same word:
Why have two kinds of quote? It lets you put one inside the other without trouble. For example, “It’s easy” works because the apostrophe is safe inside double quotes.
Give the string a name with an equals sign, then use the name.
Triple quotes, either three single or three double, let a string run across several lines. Line breaks inside become part of the string.
Concatenation means joining strings with a plus sign. Python adds nothing between them, so add a space yourself if you want one.
You can also repeat a string with a star:
Strings behave like an ordered collection of characters, so you can pick one character by its position. They are not Python arrays, but they do support indexing in the same way. Here is how python is numbered:
| Letter | p | y | t | h | o | n |
|---|---|---|---|---|---|---|
| From the left | 0 | 1 | 2 | 3 | 4 | 5 |
| From the right | -6 | -5 | -4 | -3 | -2 | -1 |
A single character is itself a string of length 1. A string that starts with a space gives a space at position 0, so check for stray spaces when an index looks wrong.
Try to change a character in place and Python refuses:
To get a changed version, build a new string, for example "P" + a[1:] gives Python.
Slicing cuts a piece out of a string. The pattern is string[start:end:step]. The start position is included, and the end position is not. Leave out a number and Python uses the beginning or end. Negative numbers count from the right.
| Slice | Meaning | Result for python |
|---|---|---|
| a[2:] | From position 2 to the end | thon |
| a[1:4] | Positions 1, 2 and 3 | yth |
| a[-4:-2] | From 4th last up to, not including, 2nd last | th |
| a[:3] | From the start up to, not including, 3 | pyt |
| a[::2] | Every second character | pto |
| a[::-1] | Step backward, which reverses the string | nohtyp |
Strings come with many built in methods. A method is called with a dot after the string. Python is case sensitive, so the names are all lowercase, strip and not Strip. Remember that methods do not change the original string. They return a new one.
strip removes spaces, tabs and new lines from both ends. lstrip does the left end only, and rstrip the right end only. Here the repr function shows the spaces:
len is a function, not a string method. It counts the characters, including spaces.
They change the case of every letter, and keep spaces.
title makes the first letter of every word a capital. capitalize does it only for the first letter of the whole string.
replace swaps every matching part of the string for new text.
format fills the curly braces in a string with values. The value can be a number, text or anything else, not only a number.
Modern Python code usually uses f strings instead. Put the letter f before the opening quote and write the name inside braces. You can even do a calculation or set the format inside the braces.
count tells you how many times a piece of text appears.
Both give the position where the text first appears. The difference is what happens when it is not found. find returns minus 1, and index raises a ValueError.
The first is at position 2, inside the word This.
split breaks a string into a list of pieces at the text you give. With no argument, it splits on spaces. join does the opposite. It glues a list of strings together, using the string it is called on as the glue.
These check how a string begins or ends. Note that startswith checks only the beginning, so it does not test whether a character is present anywhere.
To check whether a character or phrase appears anywhere in a string, use the in word. Use not in for the opposite. Both give True or False.
A backslash gives a special meaning to the character after it. It lets you put quotes inside a string and add new lines. A raw string, with an r before the quote, turns this off, which is useful for file paths.
| Method | What it does | Example result |
|---|---|---|
| strip() | Removes spaces from both ends | ‘Hello python’ |
| lower(), upper() | Changes case | hello python |
| title() | Capital letter on each word | Hello Python |
| replace(old, new) | Swaps matching text | Free |
| find(text) | Position of text, or minus 1 | 2 |
| index(text) | Position of text, or an error | 2 |
| count(text) | How often text appears | 4 |
| split(sep) | Breaks into a list | [‘P’, ‘ima’, ‘y’] |
| join(list) | Glues a list into a string | to-be-or-not |
| startswith(), endswith() | Checks the beginning or end | True |
text.upper() alone changes nothing. Save it with text = text.upper().x + y gives wirelessnetwork.strip, not Strip.find, or check with in first."Score: " + 85 is a TypeError. Use an f string.The program below holds every example in this tutorial. Copy it into a file, run it, and compare the output with what you see above.
Strings are everywhere in Python, and now you know how to create them, cut them, search them and clean them. The best way to learn is to type each example, change it, and see what happens. When you are comfortable, move on to lists, tuples and dictionaries, which store collections of values, and then to reading and writing text files.