Using str.len() to Find String Lengths in a DataFrame
📖 Scenario: You work at a small bookstore. You have a list of book titles and want to find out how many letters each title has. This helps you decide how much space each title will take on the shelf labels.
🎯 Goal: Build a small program that creates a list of book titles, then uses str.len() to find the length of each title. Finally, display the lengths alongside the titles.
📋 What You'll Learn
Create a pandas DataFrame with a column named
Title containing the exact book titles.Create a variable called
title_lengths that stores the length of each title using str.len().Add a new column called
Length to the DataFrame using title_lengths.Print the DataFrame to show the book titles alongside their lengths.
💡 Why This Matters
🌍 Real World
Bookstores and libraries often need to manage book titles and labels. Knowing the length of titles helps in designing labels and organizing shelves.
💼 Career
Data analysts and data scientists use string operations like <code>str.len()</code> to clean and analyze text data in many industries, including retail and publishing.
Progress0 / 4 steps