2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Pythonの文字列フォーマット

Posted at

format

辞書

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

a is aa
b is bb


### 左寄せ、中央寄せ、右寄せ

```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


### 左寄せ、中央寄せ、右寄せ(0埋め)
```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


### 左寄せ、中央寄せ、右寄せ(文字埋め)
```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


### 桁区切り、小数点
```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文字列

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

a is aa
b is bb

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?