There are two string formatting schemes in Python, one is the % operator that existed before Python 2.6, and the other is a new string formatting function str.format() that has been added since Python 2.6.
% Formatting Tool
Students familiar with the C printf() function will easily learn the %formatting tool. print() uses conversion specifiers beginning with % to format output for various types of data.
Output of integers.
- %o – octal octal
- %d – dec decimal
- %x – hex hexadecimal
Output of floating point numbers.
- %f – retains six significant digits after the decimal point. %.3f, retains 3 decimal places
- %e – retains six significant digits after the decimal point, output in exponential form. %.3e, retains three decimal places, uses scientific notation
- %g – use decimal form if six valid digits are guaranteed, otherwise use scientific notation %.3g, retaining 3 significant digits, using decimal or scientific notation
String output.
- %s – output string
- %10s – right justified, placeholder 10 bits
- %-10s – left justified, placeholder 10 bits
- %.2s – intercept 2-bit string
- %10.2s – 10-bit placeholder, two-bit string intercepted
|
|
format function
str.format() enhances string formatting. The basic syntax is via {} and : instead of the previous %. format function can take unlimited arguments and positions can be out of order.
Positional matching.
- without numbering, i.e. “{}”
- with numbering, i.e. “{1}”, “{2}”
- with keywords, i.e. “{a}”, “{tom}”
Format conversion.
- ‘b’ - binary. Outputs the number in base 2.
- ‘c’ - Character. Converts integers to their corresponding Unicode strings before printing.
- ’d’ - Decimal integer. Outputs the number in base 10.
- ‘o’ - Octal. Outputs the number in base 8.
- ‘x’ - Hexadecimal. Outputs numbers in base 16, with lowercase letters for digits above 9.
- ’e’ - Power notation. Print numbers in scientific notation. Use ’e’ for powers.
- ‘g’ - General format. Outputs the value in fixed-point format. When the value is particularly large, print it in power format.
- ’n’ - Numeric. Same as ’d’ when the value is an integer, same as ‘g’ when the value is a floating-point number. The difference is that it inserts a number separator depending on the region setting.
- ‘%’ - percent. Multiplies the value by 100 and prints it in fixed-point(‘f’) format, the value will be followed by a percent sign.
Left-center-right alignment and bit-completion.
- < (default) left-aligned, > right-aligned, ^ middle-aligned, = (for numbers only) to round up after the decimal point
- Take digits “{:4s}”, “{:.2f}”, etc.
Code examples and more.
|
|
String interpolation F-strings
In Python version 3.6.2, PEP 498 introduced a new string formatting mechanism, called “string interpolation” or more commonly known as F-strings, which provides an explicit and convenient way to embed python expressions into strings to format them.
To create an f string, prefix the string with the letter " f". The string itself is formatted as str.format(). f strings provide a concise and convenient way to format python expressions embedded in string literals.
Extension: String Templates
Template, set a string as a template, and finally get the desired string by replacing variables.
Usage examples.