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 same
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
F-string were introduced in Python 3.6
str.format()
exists since Python 3.0%-string
is old (legacy) style from Python 1 and 2 eraMore information in String Literals
>>> name = 'Mark'
>>> result = 'Hello {name}'
>>> result
'Hello {name}'
F-string (preferred):
>>> name = 'Mark'
>>> result = f'Hello {name}'
>>> result
'Hello Mark'
String format (legacy):
>>> name = 'Mark'
>>> result = 'Hello {}'.format(name)
>>> result
'Hello Mark'
>>> name = 'Mark'
>>> result = 'Hello {0}'.format(name)
>>> result
'Hello Mark'
>>> name = 'Mark'
>>> result = 'Hello {x}'.format(x=name)
>>> result
'Hello Mark'
%-format (legacy):
>>> name = 'Mark'
>>> result = 'Hello %s' % name
>>> result
'Hello Mark'
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('Hello {name}')
Hello {name}
>>> name = 'Mark'
>>> print(f'Hello {name}')
Hello Mark
2.2.4. End of Lines¶
No semicolon (
;
) at the end of linesUse
\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 = ...