avatarUmberto Grando

Summary

This webpage provides a tutorial on seven Python string manipulation tricks for data scientists.

Abstract

The webpage titled "7 Python String Tricks You Need to Know!" is a tutorial aimed at Python beginners and data scientists working with text analytics. The tutorial covers seven Python string manipulation tricks, including treating strings as lists, reversing strings, using the "in" operator, replacing text, using the string module, removing spaces, and checking if a string contains numbers. The tutorial requires Python 3.x and provides code examples for each trick. The tutorial also mentions that the code is available on the author's GitHub repository and encourages readers to support the author by subscribing to Medium using their referral link.

Bullet points

  • The tutorial covers seven Python string manipulation tricks for data scientists.
  • The tricks include treating strings as lists, reversing strings, using the "in" operator, replacing text, using the string module, removing spaces, and checking if a string contains numbers.
  • The tutorial requires Python 3.x.
  • The tutorial provides code examples for each trick.
  • The code for the tutorial is available on the author's GitHub repository.
  • The tutorial encourages readers to support the author by subscribing to Medium using their referral link.

7 Python String Tricks You Need to Know!

Boost your workflow as a Data Scientist by learning these string manipulation tricks

Hello World!

Today we are going to take a look at 7 simple Python tricks to manage strings. If you are a Data Scientist working with text analytics you need to know everything about string manipulation and the tricks we are going to see will boost your workflow.

If you just want to take a look at the code, just skip to the bottom of the post and go to my (new) GitHub repository. Also, I’ve just become a Medium Partner lately, so if you want to support me, consider subscribing using my referral link or following.

0. Requirements and Target

For this tutorial, you are going to need:

  • Python 3.x

This tutorial is targeted at:

  • Python beginners

To follow the tutorial, you’ll also need to declare this variable containing the text you are going to use:

1. Python Strings are lists (sort of)

The first thing you need to know is that in Python, strings do work like lists. This means that you can use indexing and slices to access a part of that string programmatically. In the above example, you can see that we are printing the first 5 characters of the string by using a slice ([0:5]).

Pay attention that strings are not exactly lists, so you cannot use methods like append to edit them (take a look at the example above).

2. Reverse a string

If you’ve ever needed to reverse a string, you can do that by slicing it ([::-1]). This slice returns the same string but is read from the last character. The same syntax works also for reversing a list of any type of object.

3. In operator

If you need to check if a substring is inside another string, you need to use the “in” operator. The syntax is straightforward: “substring in string”, the only thing you’ll need to pay attention to is the case of the text. Pretty much every text functionality in Python is case-sensitive. If you take a look at line six of the text above, that line will return false because of the lower “h” in the substring.

4. Replace

When you need to replace some text inside your variable, you just need to use the replace method. The first argument is going to be the text you are going to replace, while the second argument will be the text to be replaced with. Pay attention to the fact that, as we’ve seen before, this method is case-sensitive.

5. String Module

In the standard Python library, there’s a module named string. This module contains a lot of cool functions related to text processing. As you can see above, for example, you can quickly access lists of specific characters that can be useful for text analysis needs.

There are a lot of other functionalities inside the module, you can find them on the official documentation:

6. Remove spaces

When you work with data scraped from the web, you are going to find a lot of text with trailing spaces (or invisible characters). To remove these, you can quickly use the strip method contained in every string. If you just need to remove spaces on one of the sides of your text, you can use:

  • rstrip
  • lstrip

7. Find if a string contains numbers

A useful feature when you are pre-processing data for Data Science applications is checking if a string contains numbers (or vice-versa). To check if any number is present inside your string, just call the isalpha method. If it returns true, then the string does not contain any number.

As usual, the code for this post is available in my (new) GitHub repo.

If you’d like to support me, consider subscribing to Medium using my referral:

Other URLs:

Personal Website:

Social Links:

LinkedIn:

More content at PlainEnglish.io. Sign up for our free weekly newsletter. Follow us on Twitter and LinkedIn. Join our community Discord.

Python
Data Science
Machine Learning
Programming
Python Programming
Recommended from ReadMedium