Python: Immutability
Imagine we have this call:
name = 'Tirion'
print(name.upper()) # => TIRION
# What will this call print on the screen?
print(name) # => ?Calling the .upper() method returns a new value with all letters converted to upper case, but it does not change the original string. So inside the variable will be the old value: 'Tyrion'. This logic holds true for methods of all primitive types.
Instead of changing the value, you can replace it. This requires variables:
name = 'Tirion'
name = name.upper()
print(name) # => TIRIONInstructions
User input data often contains extra spaces at the end or beginning of a string. They're usually cut out using a method .strip(), for example, it was hello\n' and now it's hello'.
Update the first_name variable by writing the same value to it, but this time processed by the .strip() method. Print the result.
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 = "Grigor"
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
