str.replace()

Python · Strings

Theory

.replace(old, new) returns a copy of the string in which every occurrence of the substring old is replaced by new. 'asha@techtown.in'.replace('techtown.in', 'company.com') gives 'asha@company.com'. An optional third argument limits how many replacements happen, scanning from the left: 'a-b-a'.replace('a', 'X', 1) gives 'X-b-a'. If old is not found, you simply get an unchanged copy back, with no error.

The match is exact and case-sensitive: replacing 'A' in 'a-b-a' changes nothing. Replacing with an empty string deletes text, which is a quick way to strip commas or currency symbols before converting ('1,200'.replace(',', '') then int()). Because strings are immutable, you must assign the result: text = text.replace(...). For pattern-based replacements, such as 'any run of digits', use re.sub() from the re module.

Why we use it

Cleaning text often means swapping one fixed piece for another: fixing a domain, removing thousands separators, replacing underscores with spaces. replace() does it in one call without loops or index math.

When to use it

Use replace() for fixed, literal substitutions. Chain calls for several fixes: s.replace(',', '').replace('Rs', ''). Use re.sub() when the text to replace follows a pattern or needs case-insensitive matching.

How it works

  1. Call s.replace(old, new, count)
  2. Scan the string from the left for old
  3. Found a match and count not reached? Yes: Write new in its place and keep scanning. No: Copy the rest unchanged.
  4. Build the result as a new string
  5. Return it; the original string is unchanged

Syntax

string.replace(old, new)

Example

email = 'asha@techtown.in'
print(email.replace('techtown.in', 'company.com'))

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