String Operations in Python
Slice, split, replace, strip and format text data — essential for cleaning messy datasets
String operations are one of the most-used Python skills in data cleaning. Real-world datasets have inconsistent text: extra spaces, mixed case, concatenated fields (like "FirstName LastName" in one column), date strings in wrong formats. Python strings give you 30+ built-in methods to fix all of these.
In data analysis, you will use strings constantly: cleaning city names, extracting product codes, parsing dates, and building output messages.
Examples
Key Points
- ✓strip() removes whitespace — always use it when reading text input
- ✓lower() before comparing strings: "Delhi" == "delhi" is False without it
- ✓split() is your best friend for parsing structured text data
- ✓String slicing uses [start:end] — end index is exclusive
- ✓f-strings (Python 3.6+) are cleaner than .format() or % formatting
Practice Question
A column has values like " Mumbai " with extra spaces. Which method removes leading and trailing spaces?
Related Topics
Python ListsStore, access and manipulate collections of data with Python listsData Cleaning with PandasRename columns, fix data types, remove duplicates, and standardise messy real-world dataString and Date Operations in PandasClean text columns and extract date parts with Pandas str and dt accessors