Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Is String Mutable? | Strings
Data Types in Python
course content

Course Content

Data Types in Python

Data Types in Python

1. Getting Familiar With Numbers in Python
2. True or False?
3. Strings
4. Bring All the Topics Together

bookIs 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.

123
string = "What does it really take to be first past the post?" string[0] = 'k'
copy

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:

12345678
string = "I would like to order seafood" # I changed my mind and want to order fish now new_string = string.replace('seafood','fish') # The previous order print(string) # The current order print(new_string)
copy

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:

  1. Paris is the capital of France.
  2. Brasilia is located in South America.
  3. Monkeys eat bananas.

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 3. Chapter 12
toggle bottom row

bookIs 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.

123
string = "What does it really take to be first past the post?" string[0] = 'k'
copy

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:

12345678
string = "I would like to order seafood" # I changed my mind and want to order fish now new_string = string.replace('seafood','fish') # The previous order print(string) # The current order print(new_string)
copy

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:

  1. Paris is the capital of France.
  2. Brasilia is located in South America.
  3. Monkeys eat bananas.

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 3. Chapter 12
toggle bottom row

bookIs 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.

123
string = "What does it really take to be first past the post?" string[0] = 'k'
copy

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:

12345678
string = "I would like to order seafood" # I changed my mind and want to order fish now new_string = string.replace('seafood','fish') # The previous order print(string) # The current order print(new_string)
copy

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:

  1. Paris is the capital of France.
  2. Brasilia is located in South America.
  3. Monkeys eat bananas.

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!

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.

123
string = "What does it really take to be first past the post?" string[0] = 'k'
copy

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:

12345678
string = "I would like to order seafood" # I changed my mind and want to order fish now new_string = string.replace('seafood','fish') # The previous order print(string) # The current order print(new_string)
copy

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:

  1. Paris is the capital of France.
  2. Brasilia is located in South America.
  3. Monkeys eat bananas.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Section 3. Chapter 12
Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
some-alt