2.2. Syntax Print¶
2.2.1. String¶
Either quotes (") or apostrophes (') will work
Pick one and be consistent
Do not mix -
str
opening and closing characters must be the sameMore information in String Define
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. String Interpolation¶
String interpolation will substitute variable
More information in String Literals
>>> name = 'Mark'
>>>
>>> 'Hello {name}'
'Hello {name}'
>>>
>>> f'Hello {name}'
'Hello Mark'
Note, that adding f
in front of the string will turn on the string
interpolation - variable substitution. Without it, string will be interpreted
as it is - with curly braces and variable name in it.
2.2.3. Print¶
Prints on the screen
Print string
Print variable
Print formatted (interpolated) string
More information in Builtin Printing
Print string:
>>> print('Hello World')
Hello World
Print variable:
>>> text = 'Hello World'
>>> print(text)
Hello World
Print interpolated string:
>>> name = 'Mark'
>>> print(f'Hello {name}')
Hello Mark
2.2.4. Newlines¶
Use
\n
for newlineDo not add space after
\n
character
>>> print('Hello World')
Hello World
>>> print('Hello\nWorld')
Hello
World
>>> print('Hello\n World')
Hello
World
2.2.5. 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 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`'
"""
# with Hello and World in separate lines
# type: str
result = ...
"""
* Assignment: Syntax Print Interpolation
* Complexity: easy
* Lines of code: 2 lines
* Time: 2 min
English:
1. Define `result` with text 'Hello NAME'
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 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 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'
"""
NAME = 'Mark Watney'
# Define result with text: `Hello NAME`
# Variable NAME should be interpolated
# type: str
result = ...