2.2. Syntax Print

2.2.1. String

Either quotes (") or apostrophes (') will work. This topic will be covered in depth while talking about string type.

>>> name = 'Mark'
>>> name = "Mark"
>>> name = "Mark'
Traceback (most recent call last):
SyntaxError: unterminated string literal (detected at line 1)
>>> name = 'Mark"
Traceback (most recent call last):
SyntaxError: unterminated string literal (detected at line 1)

2.2.2. Printing Values

  • Prints on the screen

  • f-string formatting for variable substitution

  • More information in Builtin Printing

>>> print('My name... José Jiménez')
My name... José Jiménez
>>> name = 'José Jiménez'
>>> print(name)
José Jiménez
>>> name = 'José Jiménez'
>>> print('My name... {name}')
My name... {name}
>>> name = 'José Jiménez'
>>> print(f'My name... {name}')
My name... José Jiménez

2.2.3. End of Lines

  • No semicolon (;) at the end of lines

  • PEP 8 -- Style Guide for Python Code: Use \n for newline

>>> print('Hello World')
Hello World
>>> print('Hello\nWorld')
Hello
World
>>> print('Hello\n World')
Hello
 World

2.2.4. Assignments

"""
* Assignment: Syntax Print Newline
* Complexity: easy
* Lines of code: 1 lines
* Time: 2 min

English:
    1. Define `result` with text 'Hello World'
    2. 'Hello' must be in a first line
    3. 'World' must be in a second line
    4. Run doctests - all must succeed

Polish:
    1. Zdefiniuj zmienną `result` z tekstem 'Hello World'
    2. 'Hello' ma być w pierwszej linii
    3. '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 type(result) is str, \
    'Variable `result` has invalid type, should be str'

    >>> 'Hello' in result
    True
    >>> '\\n' in result
    True
    >>> 'World' in result
    True
"""

# str: with Hello and World in separate lines
result = ...

"""
* Assignment: Syntax Print Interpolation
* Complexity: easy
* Lines of code: 2 lines
* Time: 2 min

English:
    1. Define `result` with text 'Hello XXX'
    2. Insted `NAME` substitute "Mark Watney"
    3. To substitute use f-string notation and `{variable}`
    4. Run doctests - all must succeed

Polish:
    1. Zdefiniiuj `result` z tekstem 'Hello NAME'
    2. W miejsce `NAME` podstaw "Mark Watney"
    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 type(result) is str, \
    'Variable `result` has invalid type, should be str'
    >>> assert 'Mark Watney' in result, \
    'Variable `result` does not contain string "Mark Watney"'
    >>> assert '{NAME}' not in result, \
    'You must use f-string'

    >>> result
    'Hello Mark Watney'
"""

# str: with Mark Watney
NAME = 'Mark Watney'

# str: with Hello NAME
result = ...