[Go to site: main page, start]

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 43 min read
18 screencasts · 57 min watch
01
Strings
3 min read Screencast available

Strings are used to store text-based data.

Read
Read
02
Python's String Methods
14 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
Read
03
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
Read
04
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
Read
05
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
Read
06
Multiline strings
2 min read Screencast available

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

Read
Read
07
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
Read
08
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
Read
09
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
Read
10
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
Read
11
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
Read
12
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
Read
13
Self-concatenation
3 min read Screencast available

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

Read
Read
14
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
Read
15
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
Read
16
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
Read
17
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
Read
18
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
Read
19
Wrapping text output
4 min read Screencast available

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

Read
Read
20
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
Read
21
Uppercasing and lowercasing
3 min read Screencast available Premium

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

Read
Read
22
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
Read
23
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
Read
Continue exploring
Profile picture of Trey
Learn something new about Python every week

My name is Trey Hunner. I publish new Python articles and screencasts every week through Python Morsels. If you want to learn something new about Python every week, join Python Morsels!

Join Python Morsels ✨