Python string format

Python string format

It is troublesome to look at the document again, so make a note of only the necessary parts.

Alignment

>>> '{:<30}'.format('left aligned')
'left aligned                  '
>>> '{:>30}'.format('right aligned')
'                 right aligned'
>>> '{:^30}'.format('centered')
'           centered           '
>>> '{:*^30}'.format('centered')
'***********centered***********'

Base 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'

Integer 3-digit separate

>>> "{:,}".format(1000000)
'1,000,000'

Dictionary object attributes

>>> urldata = {"scheme":"http","netloc":"qiita.com","path":"/drafts"}
>>> url = "{scheme}://{netloc}{path}".format(urldata)

Class attributes

** 0. ** is important.

>>> url = "{0.scheme}://{0.netloc}{0.path}".format(urldata)

Special format

>>> import datetime
>>> d = datetime.datetime(2010, 7, 4, 12, 15, 58)
>>> '{:%Y-%m-%d %H:%M:%S}'.format(d)
'2010-07-04 12:15:58'

Recommended Posts

Python string format
Python string format
String format
String format 2
Python string
Python indentation and string format
[Python 2/3] Parse the format string
String format with Python% operator
Python: String concatenation
python string slice
Python2 string type
Python # string type
format in python
Python string inversion
python variable expansion, format
String manipulation in python
[Python] Multi-line string assignment
Python string manipulation master
Image format in Python
[Python2] Date string-> UnixTime-> Date string
Python Pickle format notes
[Python] format methodical use
Random string generation (Python)
Python3> documentation string / docstring
Python string processing illustration
String to Unicode Escape Sequence Format for Python
Easily format JSON in Python
String object methods in Python
[Python] Use a string sequence
Various Python built-in string operations
About Python string comparison operators
String date manipulation in Python
Format json with Vim (with python)
python> datetime> From date string (ISO format: 2015-12-09 12:40:08) to datetime type
Python f character (formatted string)
[Python3] Format the character string using the variable name as the key.
Python Application: Data Handling Part 3: Data Format
Automatically format Python code in Vim
Handle NetCDF format data in Python
Handle GDS II format in Python
python datetime format quick reference table
String replacement with Python regular expression
6 ways to string objects in Python
python string processing map and lambda
Create a random string in Python
[Python] Format when to_csv with pandas
Format when passing a long string as an argument of python
kafka python
Conversion of string <-> date (date, datetime) in Python
Python basics ⑤
python + lottery 6
Python Summary
Python comprehension
Python technique
Studying python
Python memorandum
Python FlowFishMaster
Python service
python tips
python function ①
Python basics