The format() method has been introduced in Python3 for handling complex string formatting more efficiently. This method of the built-in string class provides functionality for complex variable substitutions and value formatting. Sometimes we may want to construct strings from other information. This is where the format() method is useful.
# Learning Formating Methods |
Output
A string can use certain specifications and subsequently, the format method can be called to substitute those specifications with corresponding arguments to the format method. The numbers in the format example are optional, so you could have also written as:
# Learning Formating Methods |
What Python does in the format method is that it substitutes each argument value into the place of the specification. There can be more detailed specifications:
sum=149 |
Let’s look at more examples of format function
1. Format can manage the number of decimal numbers (float type in Python)
# Learning Formatting Methods with Numbers |
Output
0.333 ___hello___ hello______ ______hello Juan Manuel Santos wins Nobel Peace prize 2016 Abiy Ahmed Ali wins Nobel Peace prize 2019 |
The width number reserves the place for the value to be inserted as show below:
Summary Formatting Types
Inside the placeholders you can add a formatting type to format the result:
:< Left aligns the result (within the available space) :> Right aligns the result (within the available space) :^ Center aligns the result (within the available space) := Places the sign to the left most position :+ Use a plus sign to indicate if the result is positive or negative :- Use a minus sign for negative values only : Use a space to insert an extra space before +ve numbers (and a – sign befor -ve numbers) :, Use a comma as a thousand separator :_ Use a underscore as a thousand separator :b Binary format :c Converts the value into the corresponding unicode character :d Decimal format :e Scientific format, with a lower case e :E Scientific format, with an upper case E :f Fix point number format :F Fix point number format, in uppercase format (show inf and nan as INF and NAN) :g General format :G General format (using a upper case E for scientific notations) 😮 Octal format 😡 Hex format, lower case :X Hex format, upper case :n Number format :% Percentage format |
Fstring Formatting
PEP 498 introduced a new string formatting mechanism known as Literal String Interpolation or more commonly as F-strings (because of the leading f character preceding the string literal). The idea behind f-strings is to make string interpolation simpler.
To create an f-string, prefix the string with the letter “ f ”. The string itself can be formatted in much the same way that you would with str.format(). F-strings provide a concise and convenient way to embed python expressions inside string literals for formatting.
Let’s see its implementation:
import datetime #Another way for formatting- FString |
Note : F-strings are faster than the two most commonly used string formatting mechanisms, which are % formatting and str.format().
One of Python’s coolest features is the string format operator %. This operator is unique to strings and makes up for the pack of having functions from C’s printf() family. Following is a simple example:
print(“%s has scored %d runs in %d test matches at an |
Output:
Sachin Tendular has scored 15921 runs in 200 test matches at an average of 53.80!
Format Symbol |
Conversion |
%c |
Character |
%s |
string conversion via str() prior to formatting |
%i |
signed decimal integer |
%d |
signed decimal integer |
%u |
unsigned decimal integer |
%o |
octal integer |
%x |
hexadecimal integer (lowercase letters) |