Show Menu
Cheatography

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.writ­e('­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}­­".f­o­r­ma­­t(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"...te­xt...{­var­iab­le}...t­ex­t..."
f"{v­ari­abl­e:^­n}"
center within a field of width n
f"{v­ari­abl­e:s­ymb­ole­^n}­"
f"{v­ari­abl­e:<­n}"
leftalign within a field of width n
f"{v­ari­abl­e:>­n}"
rightalign within a field of width n
f"{v­ari­abl­e:n­}"
equal to
f"{v­ari­abl­e:>­n}"
f"{v­ari­able:d}
integer =
int()
{varia­ble­:.n­f}"
floati­ng-­number with n demical places
f"{v­ari­abl­e:e­}"
scientific format
f"{v­ari­abl­e:n­e}"
scientific format with n demical places
f"{v­ar:.{i­nt}­f}"
To combine multiple formats, the order is as follows : width, demical places, scientific format
 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

          Studying Cheat Sheet
          Python_Chap_4 Cheat Sheet

          More Cheat Sheets by Theo666

          Python_Chap_2 Cheat Sheet
          Python_Chap_4 Cheat Sheet
          Chap_7 Cheat Sheet