Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Replacing | String Methods
String Manipulation in Python
course content

Course Content

String Manipulation in Python

String Manipulation in Python

1. Basic Concepts
2. String Methods
3. Strings Formatting

bookReplacing

Let's consider one more possible need when we need to replace some parts of the text.

Surely, Python can handle this. There is a built-in method .replace with 2 required arguments: oldvalue (what needs to be replaced) and newvalue (and replaced by what). For example, in the string "Hello, world!" we want to replace world by friend.

12
print("Hello, world!") print("Hello, world!".replace('world', 'friend'))
copy

Note

  • String is an immutable data type in Python, accordingly, the methods we will learn will not change value of string, and will only return a copy of the string.

  • You can apply .replace sequentially (s.replace('a', 'b').replace('c', 'd')).

Task
test

Swipe to show code editor

Given string topic "Data Visualization in R". You need to replace Visualization with Manipulation, and R with Python, save the result in new_topic, and print it.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 2. Chapter 2
toggle bottom row

bookReplacing

Let's consider one more possible need when we need to replace some parts of the text.

Surely, Python can handle this. There is a built-in method .replace with 2 required arguments: oldvalue (what needs to be replaced) and newvalue (and replaced by what). For example, in the string "Hello, world!" we want to replace world by friend.

12
print("Hello, world!") print("Hello, world!".replace('world', 'friend'))
copy

Note

  • String is an immutable data type in Python, accordingly, the methods we will learn will not change value of string, and will only return a copy of the string.

  • You can apply .replace sequentially (s.replace('a', 'b').replace('c', 'd')).

Task
test

Swipe to show code editor

Given string topic "Data Visualization in R". You need to replace Visualization with Manipulation, and R with Python, save the result in new_topic, and print it.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 2. Chapter 2
toggle bottom row

bookReplacing

Let's consider one more possible need when we need to replace some parts of the text.

Surely, Python can handle this. There is a built-in method .replace with 2 required arguments: oldvalue (what needs to be replaced) and newvalue (and replaced by what). For example, in the string "Hello, world!" we want to replace world by friend.

12
print("Hello, world!") print("Hello, world!".replace('world', 'friend'))
copy

Note

  • String is an immutable data type in Python, accordingly, the methods we will learn will not change value of string, and will only return a copy of the string.

  • You can apply .replace sequentially (s.replace('a', 'b').replace('c', 'd')).

Task
test

Swipe to show code editor

Given string topic "Data Visualization in R". You need to replace Visualization with Manipulation, and R with Python, save the result in new_topic, and print it.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Let's consider one more possible need when we need to replace some parts of the text.

Surely, Python can handle this. There is a built-in method .replace with 2 required arguments: oldvalue (what needs to be replaced) and newvalue (and replaced by what). For example, in the string "Hello, world!" we want to replace world by friend.

12
print("Hello, world!") print("Hello, world!".replace('world', 'friend'))
copy

Note

  • String is an immutable data type in Python, accordingly, the methods we will learn will not change value of string, and will only return a copy of the string.

  • You can apply .replace sequentially (s.replace('a', 'b').replace('c', 'd')).

Task
test

Swipe to show code editor

Given string topic "Data Visualization in R". You need to replace Visualization with Manipulation, and R with Python, save the result in new_topic, and print it.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Section 2. Chapter 2
Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
We're sorry to hear that something went wrong. What happened?
some-alt