2.4. Syntax Print
2.4.1. Summary
print()
- Print on the screenEither quotes (") or apostrophes (') will work
String interpolation will substitute variable
Use
\n
for newline
2.4.2. String Interpolation
String interpolation will substitute variable
More information in String Literals
Adding f
in front of the string will turn on the string
interpolation - variable substitution.
>>> name = 'Mark'
>>>
>>> f'Hello {name}'
'Hello Mark'
Without it, string will be interpreted as it is - with curly braces and variable name in it.
>>> name = 'Mark'
>>>
>>> 'Hello {name}'
'Hello {name}'
Even if you use f-string, but your variable is not in proper brackets, it will be not interpolated.
>>> name = 'Mark'
>>>
>>> f'Hello (name)'
'Hello (name)'
2.4.3. Print
Prints on the screen
Print string
Print variable
Print formatted (interpolated) string
More information in Builtin Printing
Print string:
>>> print('Mark Watney')
Mark Watney
Print variable:
>>> text = 'Mark Watney'
>>> print(text)
Mark Watney
Print interpolated string:
>>> firstname = 'Mark'
>>> lastname = 'Watney'
>>>
>>> print(f'Hello {firstname} {lastname}!')
Hello Mark Watney!
2.4.4. Newline
Use
\n
for newlineDo not add space after
\n
character
>>> print('Mark Watney')
Mark Watney
>>> print('Mark\nWatney')
Mark
Watney
Mind, not to add additional space after the newline character. This will cause Python to print a newline and then space character:
>>> print('Mark\n Watney')
Mark
Watney
2.4.5. Recap
print()
- Print on the screenEither quotes (") or apostrophes (') will work
String interpolation (f-string) will substitute variable
Use
\n
for newline
2.4.6. Assignments
# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`
# %% About
# - Name: Syntax Print String
# - Difficulty: easy
# - Lines: 1
# - Minutes: 2
# %% English
# 1. Define `result` with text 'Hello World'
# 2. Run doctests - all must succeed
# %% Polish
# 1. Zdefiniuj zmienną `result` z tekstem 'Hello World'
# 2. Uruchom doctesty - wszystkie muszą się powieść
# %% Hints
# - Either quotes `"` or apostrophes `'` will work
# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'
>>> assert result is not Ellipsis, \
'Assign your result to variable `result`'
>>> assert type(result) is str, \
'Variable `result` has invalid type, should be str'
>>> assert 'Hello' in result, \
'Word `Hello` must be in the `result`'
>>> assert 'World' in result, \
'Word `World` must be in the `result`'
>>> from pprint import pprint
>>> pprint(result)
'Hello World'
"""
# Define `result` with text 'Hello World'
# type: str
result = ...
# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`
# %% About
# - Name: Syntax Print Newline
# - Difficulty: easy
# - Lines: 1
# - Minutes: 2
# %% English
# 1. Define `result` with text 'Hello World'
# 2. Word 'Hello' must be in a first line
# 3. Word 'World' must be in a second line
# 4. Run doctests - all must succeed
# %% Polish
# 1. Zdefiniuj zmienną `result` z tekstem 'Hello World'
# 2. Słowo 'Hello' ma być w pierwszej linii
# 3. Słowo 'World' ma być w drugiej linii
# 4. Uruchom doctesty - wszystkie muszą się powieść
# %% Hints
# - Either quotes `"` or apostrophes `'` will work
# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'
>>> assert result is not Ellipsis, \
'Assign your result to variable `result`'
>>> assert type(result) is str, \
'Variable `result` has invalid type, should be str'
>>> assert 'Hello' in result, \
'Word `Hello` must be in the `result`'
>>> assert '\\n' in result, \
'Newline `\\n` must be in the `result`'
>>> assert 'World' in result, \
'Word `World` must be in the `result`'
>>> from pprint import pprint
>>> pprint(result)
'Hello\\nWorld'
"""
# Define `result` with text 'Hello World'
# Word 'Hello' must be in a first line
# Word 'World' must be in a second line
# type: str
result = ...
# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`
# %% About
# - Name: Syntax Print Interpolation
# - Difficulty: easy
# - Lines: 1
# - Minutes: 2
# %% English
# 1. Define `result` with text 'Hello NAME'
# 2. Instead `NAME` substitute 'Mark'
# 3. To substitute use f-string notation and `{variable}`
# 4. Run doctests - all must succeed
# %% Polish
# 1. Zdefiniuj `result` z tekstem 'Hello NAME'
# 2. W miejsce `NAME` podstaw 'Mark'
# 3. Do podstawienia użyj notacji f-string i `{variable}`
# 4. Uruchom doctesty - wszystkie muszą się powieść
# %% Hints
# - Either quotes `"` or apostrophes `'` will work
# - Use f-string
# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'
>>> assert result is not Ellipsis, \
'Assign your result to variable `result`'
>>> assert type(result) is str, \
'Variable `result` has invalid type, should be str'
>>> assert 'Mark' in result, \
'Variable `result` does not contain string "Mark"'
>>> assert '{NAME}' not in result, \
'You must use f-string'
>>> from pprint import pprint
>>> pprint(result)
'Hello Mark'
"""
NAME = 'Mark'
# Define `result` with text 'Hello NAME'
# Instead `NAME` substitute "Mark"
# To substitute use f-string notation and `{variable}`
# type: str
result = ...