2.4. Syntax Print
print()- Print on the screenUse
\nfor newlineString interpolation will substitute variable
2.4.1. Print
Prints on the screen
Print string
Print string:
>>> print('Hello World')
Hello World
2.4.2. Newline
Use
\nfor newlineDo not add space after
\ncharacter
>>> print('Hello World')
Hello World
>>> print('Hello\nWorld')
Hello
World
Mind, not to add additional space after the newline character. This will cause Python to print a newline and then space character:
>>> print('Hello\n World')
Hello
World
2.4.3. Print variable
Print variable
>>> name = 'Alice'
>>>
>>> print(name)
Alice
2.4.4. String Interpolation
String interpolation will substitute variable
Interpolation is a feature of str, not print
More information in String Literals
Adding f in front of the string will turn on the string
interpolation - variable substitution.
>>> name = 'Alice'
>>>
>>> print(f'Hello {name}')
Hello Alice
Without it, string will be interpreted as it is - with curly braces and variable name in it.
>>> name = 'Alice'
>>>
>>> print('Hello {name}')
Hello {name}
Even if you use f-string, but your variable is not in proper brackets, it will be not interpolated.
>>> name = 'Alice'
>>>
>>> print(f'Hello (name)')
Hello (name)
2.4.5. Recap
print()- Print on the screenString interpolation (f-string) will substitute variable
Use
\nfor newlineMore information in Builtin Printing
2.4.6. Assignments
# %% About
# - Name: Syntax Print String
# - Difficulty: easy
# - Lines: 1
# - Minutes: 2
# %% 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
# %% 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ść
# %% Expected
# >>> print(result)
# Hello World
# %% Hints
# - Either quotes `"` or apostrophes `'` will work
# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python has an is invalid version; expected: `3.9` or newer.'
>>> assert result is not Ellipsis, \
'Variable `result` has an invalid value; assign result of your program to it.'
>>> assert type(result) is str, \
'Variable `result` has an invalid type; expected: `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'
"""
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -f -v myfile.py`
# %% Imports
# %% Types
result: str
# %% Data
# %% Result
result = ...
# %% About
# - Name: Syntax Print Newline
# - Difficulty: easy
# - Lines: 1
# - Minutes: 2
# %% 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
# %% 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ść
# %% Expected
# >>> print(result)
# Hello
# World
# %% Hints
# - Either quotes `"` or apostrophes `'` will work
# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python has an is invalid version; expected: `3.9` or newer.'
>>> assert result is not Ellipsis, \
'Variable `result` has an invalid value; assign result of your program to it.'
>>> assert type(result) is str, \
'Variable `result` has an invalid type; expected: `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'
"""
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -f -v myfile.py`
# %% Imports
# %% Types
result: str
# %% Data
# %% Result
result = ...
# %% About
# - Name: Syntax Print Interpolation
# - Difficulty: easy
# - Lines: 1
# - Minutes: 2
# %% 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
# %% English
# 1. Define `result` with text 'Hello X'
# 2. Replace `X` with value of a variable `NAME`
# 3. Use f-string notation
# 4. Run doctests - all must succeed
# %% Polish
# 1. Zdefiniuj `result` z tekstem 'Hello X'
# 2. Podstaw za `X` wartość zmiennej `NAME`
# 3. Użyj notacji f-string
# 4. Uruchom doctesty - wszystkie muszą się powieść
# %% Expected
# >>> print(result)
# Hello Alice
# %% Hints
# - Either quotes `"` or apostrophes `'` will work
# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python has an is invalid version; expected: `3.9` or newer.'
>>> assert result is not Ellipsis, \
'Variable `result` has an invalid value; assign result of your program to it.'
>>> assert type(result) is str, \
'Variable `result` has an invalid type; expected: `str`.'
>>> assert 'Alice' in result, \
'Variable `result` does not contain string "Alice"'
>>> assert '{NAME}' not in result, \
'You must use f-string'
>>> from pprint import pprint
>>> pprint(result)
'Hello Alice'
"""
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -f -v myfile.py`
# %% Imports
# %% Types
result: str
# %% Data
NAME = 'Alice'
# %% Result
result = ...