Python: The immutability of primitive types
Imagine that we need to change a character in a string. This is what will come out of it:
first_name = 'Alexander'
first_name[0] = 'B'
# Error: TypeError: 'str' object does not support item assignmentThis happens because of the immutability of primitive types in Python - the language does not give any physical possibility to change the string. The immutability of primitive types is important for many reasons. The key reason is performance.
But sometimes we need to change a string. That's what variables are for:
first_name = 'Alexander'
first_name = 'Blexander'
print(first_name) # => BlexanderThere is a big difference between changing the value of a variable and changing the value itself. You can't change primitive types in Python, but you can change composite types. You can also change the value of a variable without any problem.
Instructions
You are given three variables with the names of different people. Make and display a word of characters in this order:
- The third character from the first line
- The second character from the second line
- The fourth character from the third line
- The fifth character of the second line
- The third character from the second line
Try using interpolation: not only whole variables can be placed inside curly brackets, but also individual characters using square brackets.
If you've reached a deadlock it's time to ask your question in the «Discussions». How ask a question correctly:
- Be sure to attach the test output, without it it's almost impossible to figure out what went wrong, even if you show your code. It's complicated for developers to execute code in their heads, but having a mistake before their eyes most probably will be helpful.
Your exercise will be checked with these tests:
import importlib
def test(capsys):
expected = "print"
expect_output(capsys, expected)
def expect_output(capsys, expected):
importlib.import_module('solution')
out, _err = capsys.readouterr()
actual = out.strip('\n')
with capsys.disabled():
print('\n')
print(out)
assert actual == expectedTeacher's solution will be available in:
20:00
