Loading topic
print() is the function most Python programs use to show results. Its defaults cover simple cases. Its three keyword arguments, sep, end and file, control nearly everything else you need from it. This chapter covers how print() turns values into text, how to control the layout, and the mis4 min readstr type. You should also be comfortable calling print(). Here you will learn how to put characters into a string literal that you can't type directly, such as a newline, a tab, or a quote that matches the string's own delimiter6 min readinput() behaves: what it returns, what it strips, how it ends, and where it can fail. It assumes you can run a Python script and know the basic types str, int, and float. It also assumes you know if statements, while loops, and basic try/except. Converting the5 min readstr, int and float. It also assumes you have used if, while, and try/except. Retry loops get their own chapter, so here we focus on what conversion accepts, what it rejects, and where it surprises people.5 min readwhile loops and basic try/except blocks, and that you know input() always returns a str. The previous chapter showed that int() converts text to a number. Here you will handle the case where that conversion fails, and you will turn the handling into a r5 min readstr, int, and float. It also assumes you have used print() and basic f-strings, and that you can write a simple class with __init__. By the end you will know when Python picks str() and when it picks repr(). You will also be abl6 min readstr, int, and float. You should also know how print() and str() versus repr() behave from earlier chapters. The examples use Python 3.12 unless a note says otherwise. Python 3.6 or later runs most of them.5 min read{name}, and that you know the basic types str, int, and float. Here you will learn what goes after the colon in {value:spec}. That part is called the format specification mini-language. The str.format() m4 min read= specifier in f-strings. It prints an expression's source text next to its value, which makes it a fast replacement for hand-written debug prints. It assumes you can write basic f-strings and know the !r conversion and the : format spec from the earlier chapters. It al5 min read{price:>10,.2f}, and that you know the basic types str, int, float, dict and list. If those are new, read the f-string chapters first. str.format() uses the same format spec mini-language, so most of what you alread5 min readstr, int, and float, tuples and dictionaries, and try/except. You should also have read the earlier chapters on f-strings, format specifications, and str.format(). You will probably never write new code with the % operator. You will read it, though, in old5 min readstr.format(), and % formatting from the earlier chapters. It also assumes you can use dictionaries, try/except, and keyword arguments. By the end you should be able to fill in templates written by someone else without giving that person access to your5 min readprint() and input() use them, and how a shell can connect them to files and other programs. It assumes you can run a Python script, know str, int and float, and can read a basic try/except. You sh6 min readtry/except. It also assumes you know print(), f-strings, and the standard streams from earlier chapters. Here you will apply those skills to files on disk. You will open a file, write and read text, and avoid the mistakes that cause lost5 min readwith block, and that you know the difference between str and bytes at a basic level.5 min read