+ 2

What is string formatting in python 3? Please explain simply

20th Jul 2018, 4:33 PM
farhad gohar
farhad gohar - avatar
2 Réponses
+ 5
It means displaying values represented by variables of different datatypes in specific formats right there within a string. For example, displaying fractions with a given digit precision or in a form of a percentage and alike. Check out the official docs about it: https://docs.python.org/3.5/library/string.html#format-string-syntax Since 3.6, there is a new way of doing this - formatted string literals: https://docs.python.org/3/reference/lexical_analysis.html#f-strings
20th Jul 2018, 5:18 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 3
a = 3 b = 2 print(f"{a*b} {a+b} {3-1}") # 6 5 2 One way you can format, called "f-strings"
20th Jul 2018, 5:30 PM
Tzurumi Nakato