str.strip()

Python · Strings

Theory

.strip() returns a copy of a string with leading and trailing whitespace removed; spaces, tabs and newlines inside the text are kept. ' Asha Rao '.strip() gives 'Asha Rao'. .lstrip() trims only the left side and .rstrip() only the right, which is handy for removing the trailing newline from lines read from a file. Like every string method, it returns a new string and leaves the original unchanged.

When you pass an argument, it is treated as a set of characters, not as a word. Python keeps removing any of those characters from both ends until it hits one that is not in the set. So 'xxhixx'.strip('x') gives 'hi', but 'www.site.com'.strip('w.com') gives 'site', because w, ., c, o and m are all stripped from both ends. To remove an exact prefix or suffix, use .removeprefix() or .removesuffix() instead.

Why we use it

Stray spaces are invisible but break comparisons and joins: 'Asha ' is not equal to 'Asha'. Stripping is the first cleaning step for user input, CSV fields and lines read from files.

When to use it

Strip text fields right after reading them and before comparing, grouping or converting with int() or float(). Use rstrip('\n') for file lines. Use replace(' ', '') if you need to remove spaces inside the text too.

How it works

  1. Call s.strip()
  2. Look at the first character
  3. Is it whitespace (or in the given chars)? Yes: Drop it and check the next one. No: Stop trimming the left side.
  4. Repeat the same check from the right end
  5. Return the new trimmed string

Syntax

string.strip()
string.strip(chars)

Example

name = '   Asha Rao   '
print(name.strip())

Key points

Python topics

Getting Started

Variables & Data Types

Operators

Strings

Lists

Tuples

Sets

Dictionaries

Control Flow

Loops

Functions & OOP

Working with Data

Tooling & Errors

File Handling

Exception Handling

NumPy Library

Pandas Basics

Data Cleaning

Combining DataFrames (SQL Joins & Unions)

GroupBy & SQL-Style Analytics

Reshaping & Workflow

Data Visualization

Machine Learning (Scikit-Learn)

Advanced Python & AI

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Python for Data Analysts

From basics to business insights — the complete step-by-step Python roadmap. Click any topic in the sidebar to open it right here.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice Python Online 141 Python questions for data analysts — strings, records, dates, statistics and regex — with hidden tests and instant verdicts. Start practising →
Section {{ sec.num }}

{{ sec.label }}

Python  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs real Python 3.12 in your browser (first run loads the engine)
{{ liveOutput }}
{{ liveError }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic