Python string format

format

dictionary

hoge = {'a': 'aa', 'b': 'bb'}
print('a is {a}\nb is {b}'.format(**hoge))

a is aa b is bb


### Left justified, center justified, right justified

```python
hoge = {'a': 'left', 'b': 'center', 'c': 'right'}
print('left  : {a:<10}'.format(**hoge))
print('center: {b:^10}'.format(**hoge))
print('right : {c:>10}'.format(**hoge))

left : left center: center right : right


### Left justified, center justified, right justified (0 padded)
```python
hoge = {'a': 'left', 'b': 'center', 'c': 'right'}
print('left  : {a:<010}'.format(**hoge))
print('center: {b:^010}'.format(**hoge))
print('right : {c:>010}'.format(**hoge))

left : left000000 center: 00center00 right : 00000right


### Left justified, center justified, right justified (character padding)
```python
hoge = {'a': 'left', 'b': 'center', 'c': 'right'}
print('left  : {a:_<10}'.format(**hoge))
print('center: {b:_^10}'.format(**hoge))
print('right : {c:_>10}'.format(**hoge))

left : left______ center: center right : _____right


### Digit separator, decimal point
```python
print('{:,}'.format(1234.5678))
print('{:,.1f}'.format(1234.5678))
print('{:,.2f}'.format(1234.5678))
print('{:,.3f}'.format(1234.5678))
print('{:,.4f}'.format(1234.5678))
print('{:,.5f}'.format(1234.5678))

1,234.5678 1,234.6 1,234.57 1,234.568 1,234.5678 1,234.56780


## f character string

```python
a = 'aa'
b = 'bb'
print(f'a is {a}\nb is {b}')

a is aa b is bb



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
[python] Convert date to string
Easily format JSON in Python
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
python: Use the variables defined in the string format as they are
kafka python
Conversion of string <-> date (date, datetime) in Python
Python basics ⑤
python + lottery 6
Python Summary
Built-in python
Python comprehension
Python technique
Studying python
Python 2.7 Countdown
Python memorandum