7.3. String Print — Python
7.3.1. String Module
import string string.punctuation # '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~' string.whitespace # ' \t\n\r\x0b\x0c' string.ascii_lowercase # 'abcdefghijklmnopqrstuvwxyz' string.ascii_uppercase # 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' string.ascii_letters # 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' string.digits # '0123456789' string.hexdigits # '0123456789abcdefABCDEF' string.octdigits # '01234567' string.printable # '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~ \t\n\r\x0b\x0c'
7.3.2. print function
7.3.3. Function definition
def print(*values, sep=' ', end='\n', file=sys.stdout, flush=False): """ Prints the values to a stream, or to sys.stdout by default. Optional keyword arguments: file: a file-like object (stream); defaults to the current sys.stdout. sep: string inserted between values, default a space. end: string appended after the last value, default a newline. flush: whether to forcibly flush the stream. """ ...
7.3.4. Intuitive implementation
Intuitive implementation of print function:
def print(*values, sep=' ', end='\n', ...): return sep.join(values) + end
7.3.5. Printing multiple values
name = 'Alice' print('Hello', name, '!') # Hello Alice!
name = 'Alice' print('Hello', name, '!', sep=';') # Hello;Alice;!
7.3.6. String concatenation
+operator (with side effects)str.join()str.format()f-string formatting (preferred)
7.3.7. + Operator
f-string formatting are preferred over
stradditionHow many
strare in the memory?
name = 'Alice' 'Hello' + name # 'Hello Alice'
+ Operator side effect:
name = 'Alice' age = 30 'Hey ' + name + ' are you ' + str(age) + ' years old?' # 'Hey Alice are you 30 years old?'
7.3.8. str.join()
data = ['Alice', 'Bob', 'Carol'] ' '.join(data) # 'Alice Bob Carol' ','.join(data) # 'Alice,Bob,Carol' ', '.join(data) # 'Alice, Bob, Carol'
7.3.9. Use Case - 1
>>> ... from time import sleep ... ... ... def progressbar(percent): ... filled = '=' * percent ... empty = ' ' * (100-percent) ... clear = '\b' * 110 ... bar = f'{clear}{percent:4}% |{filled}{empty}|' ... print(bar, end='') ... ... ... for i in range(0,101): ... progressbar(i) ... sleep(0.2)
7.3.10. Assignments
# FIXME: Write tests # FIXME: Write solution # %% About # - Name: Powielanie napisów # - Difficulty: easy # - Lines: 8 # - Minutes: 5 # %% 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. Given string: `text = 'Lorem Ipsum'` # 2. Write three functions: # - `print_1(text)` using `range()` # - `print_2(text)` using `while` loop # - `print_3(text)` using string multiplication # 3. Each function should print 5 copies of this string # 4. Each string in a separate line # 5. Write doctest for all functions # 6. Run doctests - all must succeed # %% Polish # 1. Dany jest ciąg znaków: `text = 'Lorem Ipsum'` # 2. Napisz trzy funkcje: # - `print_1(text)` wykorzystującą `range()` # - `print_2(text)` wykorzystującą pętlę `while` # - `print_3(text)` wykorzystującą mnożenie stringów # 3. Każda funkcja ma wyświetlić 5 kopii tego ciągu znaków # 4. Każdy ciąg znaków w osobnej linii # 5. Napisz doctest do wszystkich funkcji # 6. Uruchom doctesty - wszystkie muszą się powieść # %% Doctests """ >>> import sys; sys.tracebacklimit = 0 >>> assert sys.version_info >= (3, 9), \ 'Python has an is invalid version; expected: `3.9` or newer.' """ # %% 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 # %% Data # %% Result result = ...
# FIXME: Write tests # FIXME: Write solution # %% About # - Name: Przeliczanie temperatury # - Difficulty: easy # - Lines: 8 # - Minutes: 13 # %% 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. Write a program that will display a table of Celsius to Fahrenheit conversions in the range from -20 to +40 degrees Celsius (every 5 degrees). # 2. The result must be as shown in the listing below # 3. The sign must always be displayed # 4. Pay attention to the text justification # 5. Pay attention to filling the space not occupied by numbers # 6. Run doctests - all must succeed # %% Polish # 1. Napisz program, który wyświetli tabelę przeliczeń stopni Celsjusza na stopnie Fahrenheita w zakresie od –20 do +40 stopni Celsjusza (co 5 stopni). # 2. Wynik musi być taki jak na listingu poniżej # 3. Znak ma być zawsze wyświetlany # 4. Zwróć uwagę na wyjustowanie tekstu # 5. Zwróć uwagę na wypełnienie miejsca niezajętego przez cyfry # 6. Uruchom doctesty - wszystkie muszą się powieść # %% Hints # - Fahrenheit to Celsius: (°F - 32) / 1.8 = °C # - Celsius to Fahrenheit: (°C * 1.8) + 32 = °F # %% Doctests """ >>> import sys; sys.tracebacklimit = 0 >>> assert sys.version_info >= (3, 9), \ 'Python has an is invalid version; expected: `3.9` or newer.' >>> result # doctest: +SKIP ------------------------------------------- | Temperature | - 20°C | ....-4....°F | ------------------------------------------- | Temperature | - 15°C | ....+5....°F | ------------------------------------------- | Temperature | - 10°C | ...+14....°F | ------------------------------------------- | Temperature | - 5°C | ...+23....°F | ------------------------------------------- | Temperature | + 0°C | ...+32....°F | ------------------------------------------- | Temperature | + 5°C | ...+41....°F | ------------------------------------------- | Temperature | + 10°C | ...+50....°F | ------------------------------------------- | Temperature | + 15°C | ...+59....°F | ------------------------------------------- | Temperature | + 20°C | ...+68....°F | ------------------------------------------- | Temperature | + 25°C | ...+77....°F | ------------------------------------------- | Temperature | + 30°C | ...+86....°F | ------------------------------------------- | Temperature | + 35°C | ...+95....°F | ------------------------------------------- | Temperature | + 40°C | ...+104...°F | ------------------------------------------- """ # %% 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 # %% Data def celsius_to_fahrenheit(degree): return degree*1.8 + 32 # %% Result result = ...