0
0
Data Analysis Pythondata~5 mins

String cleaning (strip, lower, replace) in Data Analysis Python

Choose your learning style9 modes available
Introduction

We clean text data to make it neat and consistent. This helps us analyze it better and avoid mistakes.

When user input has extra spaces before or after words.
When text data has mixed uppercase and lowercase letters.
When you want to fix or remove unwanted characters in text.
When preparing data for searching or matching text.
When standardizing text for reports or visualizations.
Syntax
Data Analysis Python
string.strip()
string.lower()
string.replace(old, new)

strip() removes spaces at the start and end of a string.

lower() changes all letters to lowercase.

replace() swaps old parts of the string with new parts.

Examples
Removes spaces around the text, result is 'Hello World'
Data Analysis Python
'  Hello World  '.strip()
Changes all letters to lowercase, result is 'python'
Data Analysis Python
'Python'.lower()
Replaces dashes with slashes, result is '2024/06/01'
Data Analysis Python
'2024-06-01'.replace('-', '/')
Combines all: trims spaces, lowercases, and replaces spaces with underscores, result is 'data_science'
Data Analysis Python
'  Data Science  '.strip().lower().replace(' ', '_')
Sample Program

This code cleans a list of fruit names by removing extra spaces, making all letters lowercase, and replacing 'a' with '@'.

Data Analysis Python
data = ['  Apple ', 'Banana ', ' CHERRY', 'daTe']

cleaned = [item.strip().lower().replace('a', '@') for item in data]

print(cleaned)
OutputSuccess
Important Notes

Remember strip() only removes spaces at the start and end, not inside the text.

lower() does not change numbers or symbols.

replace() changes all occurrences of the old string, so be careful what you replace.

Summary

Use strip() to clean spaces around text.

Use lower() to make text all lowercase for consistency.

Use replace() to fix or change parts of the text.