How to use Right function in Python

Text fields require good amount of cleaning before starting data analysis. Python Pandas has many functions that helps working with text columns in easier ways. There can be a requirement to extract letters from right side in a text value, in such case .str[] option in Python is helpful. In this article we will learn how to use right function in Python with the help of an example.

Emma has customer data available for her company. There is one Phone column available in the Dataframe. She is looking forward to extract 12 letters from right the column. For example, for “80-790-163-1507″ she is looking to create a new Phone column with value ” 790-163-1507 “.

How to use Right function in Python

Right Function in Python Dataframe

  • Step 1: Import all the necessary modules.
import pandas as pd
  • Step 2: Use str[] operation to extract letters from right. Firstly, mention the Dataframe and Field Name. str[-12:], extracts 12 letters from right, in case you need to extract 5 letters from right then use [-5:]. For the current example, syntax is:
Customer_Data['Phone Updated'] = Customer_Data['Phone'].str[-12:]
  • Step 3: Check the output data quality to assess the observations in final Dataframe.
Customer_Data.head(15)
How to use Right function in Python

Thus, Emma is able to extract letters from right as per her requirement in Python.

To get top certifications in Python and build your resume visit here. Also, you can read books listed here to build strong knowledge around Python.

Visit us below for video tutorial:

📬 Stay Ahead in Data Science & AI – Subscribe to Newsletter!

  • 🎯 Interview Series: Curated questions and answers for freshers and experienced candidates.
  • 📊 Data Science for All: Simplified articles on key concepts, accessible to all levels.
  • 🤖 Generative AI for All: Easy explanations on Generative AI trends transforming industries.

💡 Why Subscribe? Gain expert insights, stay ahead of trends, and prepare with confidence for your next interview.

👉 Subscribe here:

Related Posts