22 Pythonic Tricks for Working with Strings
Posted on July 19, 2020 in Tips & Tricks
22 Pythonic Tricks for Working with Strings
22 Pythonic Tricks for Working with Strings
String manipulation is something we Pythonistas do a lot. Here are 20 top tips & tricks you might find useful!
All these examples are using Python 3, at the time of writing the latest version is 3.8.2. If you’re new to programming, you’ll find my Introduction to Strings article useful.
0. Is a Substring in a String?
Use the in-built comparator in
which returns True
or False
1. Reverse a String
Use a slice with a decreasing step to return the reversed string.
2. Compare Two Strings for Equality
Compare equality using ==
to see if two objects have an equal value.
Compare
identity using is
to see if two objects are one
and the same.
3. Lowercase, Uppercase, Sentence Case and Title Case of a String
Python has a number of in-built functions you can use to change the case of a string.
4. Concatenate Strings Efficiently
Use join on an empty string to
concatenate the parameters.
This function receives a variable number of arguments using the
special *
indicator.
5. Is the String Empty or None?
A perfectly fine string
object can contain 0 characters, or all
whitespace, or even be the None
object.
This
function can be used if you want to test for character or byte content in the string.
6. Trim Leading and Trailing Whitespace
The following function returns a
3-tuple
, each element contains a version of the
original string
but stripped of whitespace in a
different way in each.
On line 6 you can see that Python understands statements which assign
values to multiple variables.
7. Generate a String of Random Characters
Use the secrets module to make random choices of characters to add to a string.
8. Read the Lines in a File to a List
The file reader object f
is being converted to a list implicitly
here.
Note: here we’re open the file in ‘r’
mode for read-only.
9. Tokenize a Sentence Fully
Tokenization is a procedure in natural language processing — it returns all tokens in a sentence including punctuation. We’ll use NLTK, which you’ll need to install separately.
10. Execute Python Code Found Inside a String
Here there are two methods being
displayed. exec_string
will send the contents
of a string to the operating system. eval_string
will use Python to evaluate the
string.
Be careful with both, the strings you are executing might be malicious!
11. Find the Substring Between Two Markers
Use regular expressions and F-Strings.
12. Remove all Punctuation from a String
The strings module contains a list of
punctuation characters.
Use translate (see #21) to remove them from the string.
13. Encode and Decode UTF-8 URLs
UTF-8 allows us to use extended, double-word characters — such as emojis. These need to be encoded before they can be used in a URL.
14. Use Base64 Encoding on Strings
Base64 is a method to encode binary data as a string for transmitting in text messages. You can also use it to encode UTF-8 strings.
15. Calculate the Similarity of Two Strings
Aside from using the Levenshtein
distance in nltk.edit_distance
, you could try
the difflib
library.
A value of 1.0 would
indicate equality.
16. Remove a Character at a Specific Index
Again, strings are a sequence of characters, so you can use slicing operations.
17. Convert Between CSV and List
Comma-separated values are everywhere
because Excel is the best, general-purpose solution to any data problem! ;)
Let’s take a
single of CSV line and convert it into a list of values.
Line 3 composes a list, splitting
the input line on every “,”
separator.
Now let’s do the reverse: take a list and return a CSV line.
18. Weave Two Strings
Use zip_longest
from the itertools
module to zip two strings of unequal
length.
This solution joins the output a list comprehension which concatenates i + j
.
19. Remove Unwanted Characters from a String
Use the replace function. Remember: you
can’t change the value of a string in-place — strings are immutable.
Also, see #21.
20. Find the Index Locations of a Character in a String
This solution uses a list comprehension
with a filter using if
.
21. Translate a String to Leetspeak
Use the functions maketrans
and translate
.
Have fun Pythonistas!