Articles › Strings

Strings

Regardless of what you're doing in Python, you almost certainly use strings all the time. A string is usually the default tool we reach for when we don't have a more specific way to represent our data.

23 articles · 1 hr 42 min read
18 screencasts · 57 min watch
Strings
3 min read Screencast available

Strings are used to store text-based data.

Read
Python's String Methods
13 min read

Python's strings have dozens of methods, but some are much more useful than others. Let's discuss the dozen-ish must-know string methods and why the other methods aren't so essential.

Read
Substrings in Python: checking if a string contains another string
6 min read

In Python, we usually check for substrings using the in operator instead of the index or find methods.

Read
String concatenation vs string interpolation
2 min read Screencast available

Wondering how to concatenate a string in Python? Hold on! There are two ways to build up bigger strings out of smaller strings: string concatenation (with +) and string interpolation (with f-strings).

Read
Newlines and escape sequences
6 min read Screencast available

Python allows us to represent newlines in strings using the \n "escape sequence" and Python uses line ending normalization when reading and writing with files.

Read
Multiline strings
2 min read Screencast available

Need to represent multiple lines of text in Python? Use Python's multi-line string syntax!

Read
Python f-string tips & cheat sheets
13 min read

Python's string formatting syntax is both powerful and complex. Let's break it down and then look at some cheat sheets.

Read
The string split method
2 min read Screencast available

Strings can be split by a substring separator. Usually the string split is called without any arguments, which splits on any whitespace.

Read
Convert a list to a string
3 min read Screencast available

Want to turn a list of strings into a single string? You can use the string join method to join a list of strings together (concatenating them with a delimiter of your choice).

Read
How to remove spaces
3 min read

Need to remove spaces from your string? Depending on your use case you can usually use string methods, but you may need a regular expression.

Read
Python's 2 different string representations
3 min read Screencast available

In Python, we have two different string representations: the human-readable string representation and the programmer-readable string representation (called repr and str respectively).

Read
Title-case a String
5 min read

The string title method falls down when title-casing contractions. What's a good alternative to the title method? Or is there one?

Read
Self-concatenation
3 min read Screencast available

Strings and other sequences can be multiplied by numbers to self-concatenate them.

Read
Why splitlines() instead of split("\n")?
3 min read Screencast available

To split text into lines in Python, use the splitlines() method, not the split() method.

Read
Checking for string prefixes and suffixes
3 min read Screencast available

Python's strings have methods for checking whether a string starts or ends with specific text and for removing prefixes and suffixes.

Read
Unindent multiline strings in Python with dedent
4 min read Screencast available

Need a multi-line string but don't want to include a big block of manually dedented text in your Python code? Use textwrap.dedent!

Read
Debugging with f-strings
4 min read Screencast available

If you're debugging Python code with print calls, consider using f-strings with self-documenting expressions to make your debugging a little bit easier.

Read
Implicit string concatenation
5 min read Screencast available

Python automatically concatenates adjacent string literals thanks to implicit string concatenation. This feature can sometimes lead to bugs.

Read
Wrapping text output
4 min read Screencast available

Python's textwrap module includes utilities for wrapping text to a maximum line length.

Read
T-strings: Python's Fifth String Formatting Technique?
6 min read Screencast available

Python's new t-strings may look like f-strings, but they work in a totally different way, allowing you to delay string interpolation.

Read
Uppercasing and lowercasing
3 min read Screencast available Premium

Python's strings have various case-modification methods, but upper, lower, and casefold are the only practical ones.

Read
Raw strings
2 min read Screencast available Premium

Raw strings are a way of making a string that has no escape sequences and instead interprets every backslash literally. Raw strings are often used with regular expressions in Python.

Read
Representing binary data with bytes
3 min read Screencast available

In Python, strings are used to represent text and bytes are used to represent binary data. If you end up with bytes representing text, you can decode them to get a string instead.

Read

Continue exploring

Profile picture of Trey

Learn something new about Python

My name is Trey Hunner. I publish new Python articles and screencasts through Python Morsels. If you want to keep learning new things about Python, join Python Morsels!

Join Python Morsels ✨