What are the ways to format python strings
This article mainly explains "what are the ways of formatting python strings". The content of the explanation in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn what are the ways to format python strings.
There are two ways to format strings in Python:% formatter and format.
% format character
% [(name)] [flags] [width]. [precision] typecode
(name) optional, used to select the specified key
Flags is optional, and the values to choose from are:
+ right alignment; positive numbers preceded exactly, negative numbers preceded by minus signs
-align to the left; there is no sign before a positive number and a negative sign before a negative number
Spaces are aligned to the right; positive numbers are preceded by spaces, and negative numbers are preceded by minus signs
Align 0 to the right; there is no sign before a positive number, and a negative sign before a negative number; fill the blanks with 0
Width optional, occupies width
Optional, the number of places retained after the decimal point
Required for typecode
S, get the return value of the _ _ str__ method of the incoming object and format it to the specified location
R, get the return value of the _ _ repr__ method of the incoming object and format it to the specified location
C, integer: converts a number to its unicode corresponding value, with a decimal range of 0