Course Content
Data Types in Python
1. Getting Familiar With Numbers in Python
4. Bring All the Topics Together
Data Types in Python
Is String Mutable?
You may recall something about mutable and immutable data types. Immutable cannot be changed .
But what about strings? They are immutable. We cannot change a string directly, only with special operations. We will not have knowledge of the outcome unless we make an attempt. Therefore, let us examine this example.
TypeError 😔
It is difficult to disagree with the assertion that modifying strings is essential. Just as individuals can alter their viewpoints, we should permit strings to undergo similar changes.
The replace()
method was implemented to solve this problem. It has the following syntax: string.replace(old_word, new_word)
. By the way, each method that can be implemented for string just creates new string because the current one cannot be modified. Look at the example in which I want to change seafood to fish:
Task
As I said before, practice is the key to success; hence, here is another occasion to do it!
Correct the mistakes in the sentences string1
, string2
, and string3
to make them truthful. You should receive the following results:
Paris is the capital of France
Brasilia located in South America
Monkeys eat bananas
Everything was clear?