Working with strings in Python: A cheat sheet | by Mahbubul Alam | Towards Data Science

Maybe you don’t want to be an NLP expert. But as a data scientist you will definitely have to deal with string objects; without exceptions. It may involve as simple as naming and renaming columns, looping through column names or editing other kinds of string objects present in the dataset.
Or maybe you are thinking to become an NLP expert. Either way, learning some basic string manipulation techniques will take you a long way to level up skills and make you comfortable working with text data.
There are some useful tools in Python standard library and other packages which are quite powerful. Using built-in tools and external libraries in I will cover the following in the article:
Built-in string methods
Counter module
Accessing elements
f-string literals
Regular expression
Let’s dive right in.
I’ll do a warm-up first.
You probably applied built-in len() function for counting items in a list, set, tuple or dictionary. You can use the same to check the total length of a string.
len('hello')>> 5
A string can also be cast into a list by using the list() constructor.
list('hello!')>> ['h', 'e', 'l', 'l', 'o', '!']
Concatenating two strings works just like how a normal numeric addition works in a math function.
first_string = 'hello'second_string = 'world'new_string = first_string + second_string>> 'hello world'
Similarly, you can get copies of a string just by multiplying as many times!
string = 'hello'(string + ' ') *5>> 'hello hello hello hello hello '
Like everything else, strings are objects in Python. Naturally, it comes with built-in methods which you can discover by hitting .tab key after the string:
So what exactly can you do with string methods? Some basic functionalities include converting a string to upper case or lower case, capitalization etc. Some examples are below:
# make uppercase'hello'.upper()>> 'HELLO'# make lowercase'HELLO'.lower()>> 'hello'# capitalize'hello'.capitalize()>> 'Hello'# title'hello world'.title()>> 'Hello World'
Besides basic methods, you can also call a few very useful methods. For example, you can split a string (let’s say a sentence) using a separator of your choice (e.g. a letter).
# split a word 'hello'.split(sep = 'e')>> ['h', 'llo']
The most useful is to split a sentence into a list of individual words with a space (‘ ’) separator.
sent = 'hello world'sent.split(sep = ' ')>> ['hello', 'world']
There is also a way to put the elements of a string in reverse order.
# reverse a strings = 'hello's[::-1]>> 'olleh'
If you find reversing a string not so much useful for your work, getting rid of unwanted things in a string certainly is. You can just strip() whatever you want to get rid of from a string.
# strip part of a string'hi there'.strip('hi ')>> 'there'
Replacing words or characters can also come in handy.
# replace a character'hello'.replace('l', 'L')>> 'heLLo'# replace a word'hello world'.replace('world', 'WORLD')>> 'hello WORLD'
By using join() method you can join a list of characters or words into a longer string object.
# join a list of letterss = ['h', 'e', 'l', 'l', 'o']''.join(s)>> 'hello'# join a list of words w = ['hello', 'world']'-'.join(w)>> 'hello-world'
Before closing out on string methods I just want to emphasize that if you work with Pythonpandas library, you will find built-in string methods similarly useful. If you just use.str. on a pandas object (i.e. strings), you can access all those methods.
The next important topic is counting — from counting characters to counting words and more.
The built-in method for counting is .count() which is applied to an object and you specify what you want to count as an argument. Just remember that it is case-sensitive.
# counting characterssent = 'His name is Abdul. Abdul is a good boy. Boy o boy'sent.count('a')>> 2# counting the occurance of an wordsent.count('name')>> 1
Python also comes with a Counter module with its collections library. It can count the frequency of each element in a string and returns a dictionary object.
# count ALL characters in a stringfrom collections import CounterCounter('aaaaabbbbbbxxxxxx')>> Counter({'a': 5, 'b': 6, 'x': 6})
With an additional line of code, Counter can also split the words and show word frequencies.
# counting ALL words in a sentencesent = 'His name is Abdul. Abdul is a good boy, boy o boy'word_lis= sent.split(' ')Counter(word_list)>> Counter({'His': 1,         'name': 1,         'is': 2,         'Abdul.': 1,         'Abdul': 1,         'a': 1,         'good': 1,         'boy,': 1,         'boy': 2,         'o': 1})
As a best practice, you should remove any punctuations in the string, otherwise, it can create some troubles down the line.
Associated .most_common() method returns tuples of items occurring in the highest frequencies.
Counter(word_list).most_common(3)>> [('is', 2), ('boy', 2), ('His', 1)]
Accessing individual items by index or accessing index by items is quite useful.
To access the index of a character/word:
'hello how are you'.find('how')>> 6
Or to access a character/word by index:
s = 'hi there's[3]>> 't'
Slicing a string works in a similar fashion as you’d do for a list objects.
s = 'hi there's[0:4]>> 'hi t'
You can format string in multiple ways in Python. Prior to Python 3.6 most people would use %-formatting or .format() but f-strings have made it quite easier — easy to use and easy to read. Here’s one example:
name = 'Anna'age = 23print(f"my name is {name} and I am {age} years old")>> my name is Anna and I am 23 years old
For numeric variables, you can specify the precision of the values in the format -> value:width.precision
# f string numbersdiv = 11/3print(f'the result of the division is {div:2.3f}') >> the result of the division is 3.667
The regular expression reis a very powerful library in Python and so much you can do with it especially for searching for patterns in a dataset. Below is a small sample of its capabilities.
If you wish to search for a matching pattern of text:
import retext = 'hello my phone number is (000)-111-2222'pattern = 'phone're.search(pattern, text)>> <re.Match object; span=(9, 14), match='phone'>
In the output, span tells the index position of the match (starting at index 6, ending at 8 in this example).
You can also search for number patterns in a specific format that you think is most likely present.
text = 'my zipcode is 22202'pattern = r'\d\d\d\d\d're.search(pattern, text)>> <re.Match object; span=(14, 19), match='22202'>
And finally, there is the so-called “wildcard”, represented by the .(dot) metacharacter.
# input texttext = 'the dog and the hog sat in the fog'# regex searchre.findall(r'.og', text)>> ['dog', 'hog', 'fog']
To summarise everything that I’ve presented in this article:
built-in string methods for basic functionalities such as changing cases, splitting, joining, replacing etc.
counting string elements using built-in .count() method or by using the Counter module
accessing string patterns and index values
the use of f-string literals for string formating
applying regular expressions re module for pattern search.
I left many other methods and functionalities, but I hope this gives a good start to build a cheat sheet to work with string objects.
If you have comments feel free to write them down below or connect with me via Medium, Twitter or LinkedIn.