Cheatography
https://cheatography.com
Affichage : print & f-string
Function print()
value |
One or more values to be printed. These can be of any data type, like str, list, etc |
sep |
Separator between each value. A space by default |
end |
String to be printed at the end of the output. '\n' by default |
file |
Where the output will be printed. the console by default |
print(value, ..., sep, end, file, flush)
print('Hello, world!', file=f, end = "")
equal to f.write('Hello, world!')
f-string (old version)
% under version 2.6 |
"%s ... text ... %d ...text... %.2f" % (var1, var2, var3)
|
the % symbol is used as a string formatting operator. The tuple at the end is applied. %s for string variable; %d for an integer; %.nf for float with n demical places |
.format() version 2.7 - 3.5 |
"{} ... {.6e} ... {:.2f}".format(var1, var2, var3)
|
the {} symbol is used as a string formatting operator. format() at the end. {} for an integer, {.6e} for a number in scientific format with 6 demical places, etc |
|
|
f-string version 3.5+
f"...text...{variable}...text..."
|
|
center within a field of width n |
f"{variable:symbole^n}"
|
|
leftalign within a field of width n |
|
rightalign within a field of width n |
|
equal to f"{variable:>n}"
|
|
|
|
floating-number with n demical places |
|
scientific format |
|
scientific format with n demical places |
|
To combine multiple formats, the order is as follows : width, demical places, scientific format
|
Created By
Metadata
Comments
No comments yet. Add yours below!
Add a Comment
Related Cheat Sheets
More Cheat Sheets by Theo666