Various format specifications of str.format () method of Python3

I reviewed the reference of the str.format () method again, so I made a lot of notes.

Specify the width of the text

Specify the number of digits after :. You can also specify the character alignment by inserting one of <,>, ^'between: `and the number.

>>> '{:30}'.format('30chars')			#The default is left justified
'30chars                       '
>>> '{:<30}'.format('left aligned')		# '<'Left justified with
'left aligned                  '
>>> '{:>30}'.format('right aligned')	# '>'Right-aligned with
'                 right aligned'
>>> '{:^30}'.format('centered')			# '^'Centered with
'           centered           '
>>> '{:*^30}'.format('centered')		#Fill in the blanks with the characters you placed before specifying the alignment
'***********centered***********'

Various numerical values

>>> '{:+f}; {:+f}'.format(3.14, -3.14)	# '+'Always display the sign
'+3.140000; -3.140000'
>>> '{: f}; {: f}'.format(3.14, -3.14)	# ' 'If positive' 'If negative'-'Show
' 3.140000; -3.140000'
>>> '{:-f}; {:-f}'.format(3.14, -3.14)	# '-'If, only the negative sign is displayed
'3.140000; -3.140000'
>>> '{:.4f}; {:.4f}'.format(3.14, -3.14)	# '.'Specify the number of digits after the decimal point after
'3.1400; -3.1400'
>>> '{:,}'.format(1234567890)	#Separated by commas by 3 digits
'1,234,567,890'
>>> 
>>> points = 19
>>> total = 22
>>> 'Correct answers: {:.2%}'.format(points / total)	#Display as a percentage
'Correct answers: 86.36%'

Bottom conversion

Specify one of d, x, o, b after: . Add # to display the prefix.

symbol Conversion destination
d Decimal number
x Hexadecimal
o 8 base
b Binary number
>>> 'int: {0:d}; hex: {0:x}; oct: {0:o}; bin: {0:b}'.format(42)
'int: 42; hex: 2a; oct: 52; bin: 101010'
>>> 'int: {0:d}; hex: {0:#x}; oct: {0:#o}; bin: {0:#b}'.format(42)
'int: 42; hex: 0x2a; oct: 0o52; bin: 0b101010'

Date format

>>> import datetime
>>> d = datetime.datetime.now()
>>> '{:%Y-%m-%d %H:%M:%S}'.format(d)
'2016-03-17 17:33:11'

Recommended Posts

Various format specifications of str.format () method of Python3
Various processing of Python
About various encodings of Python 3
[python] -1 meaning of numpy's reshape method
Summary of various for statements in Python
1. Statistics learned with Python 1-3. Calculation of various statistics (statistics)
[Python] Various combinations of strings and values
Introduction of Python
Python string format
Clustering of clustering method
[python] Create a list of various character types
Basics of Python ①
1. Statistics learned with Python 1-2. Calculation of various statistics (Numpy)
Basics of python ①
Various settings of Python static blog generation tool'Pelican'
# 3 [python3] Various operators
Copy of python
Python string format
format in python
Johnson method (python)
[Python] Semi-Lagrange method
Introduction of Python
Old openssl causes problems in various parts of python
Destroy the intermediate expression of the sweep method with Python
[Python] Summary of table creation method using DataFrame (pandas)
[Python] Operation of enumerate
List of python modules
parallelization of class method
python variable expansion, format
Unification of Python environment
Copy of python preferences
Kernel Method with Python
Basics of Python scraping basics
Various Python visualization tools
the zen of Python
Image format in Python
Installation of Python 3.3 rc1
Python Pickle format notes
[Python] format methodical use
Simplex method (simplex method) in Python
Private method in python
# 4 [python] Basics of functions
Basic knowledge of Python
Sober trivia of python3
Summary of Python arguments
Basics of python: Output
Installation of matplotlib (Python 3.3.2)
Application of Python 3 vars
Summary of test method
A simple Python implementation of the k-nearest neighbor method (k-NN)
I measured various methods of interprocess communication in multiprocessing of python3