Strings are used to store text-based data.
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.
In Python, we usually check for substrings using the in operator instead of the index or find methods.
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).
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.
Need to represent multiple lines of text in Python? Use Python's multi-line string syntax!
Python's string formatting syntax is both powerful and complex. Let's break it down and then look at some cheat sheets.
Strings can be split by a substring separator. Usually the string split is called without any arguments, which splits on any whitespace.
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).
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.
In Python, we have two different string representations: the human-readable string representation and the programmer-readable string representation (called repr and str respectively).
The string title method falls down when title-casing contractions. What's a good alternative to the title method? Or is there one?
Strings and other sequences can be multiplied by numbers to self-concatenate them.
To split text into lines in Python, use the splitlines() method, not the split() method.
Python's strings have methods for checking whether a string starts or ends with specific text and for removing prefixes and suffixes.
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!
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.
Python automatically concatenates adjacent string literals thanks to implicit string concatenation. This feature can sometimes lead to bugs.
Python's textwrap module includes utilities for wrapping text to a maximum line length.
Python’s new t-strings may look like f-strings, but they work in a totally different way, allowing you to delay string interpolation.
Python's string have various case-modification methods, but upper, lower, and casefold are the only practical ones.
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.
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.
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!