In [1]:
a = 123

In [2]:
b = 'abc'

In [3]:
print('{} and {}'.format(a, b))


123 and abc

In [4]:
print('{first} and {second}'.format(first=a, second=b))


123 and abc

In [5]:
print(f'{a} and {b}')


123 and abc

In [6]:
print(F'{a} and {b}')


123 and abc

In [7]:
print(f"{a} and {b}")


123 and abc

In [8]:
print(f'''{a} and {b}''')


123 and abc

In [9]:
print(f"""{a} and {b}""")


123 and abc

In [10]:
s = 'abc'

In [11]:
print(f'right : {s:_>8}')
print(f'center: {s:_^8}')
print(f'left  : {s:_<8}')


right : _____abc
center: __abc___
left  : abc_____

In [12]:
i = 1234

In [13]:
print(f'zero padding: {i:08}')


zero padding: 00001234

In [14]:
print(f'comma: {i:,}')


comma: 1,234

In [15]:
print(f'bin: {i:b}')
print(f'oct: {i:o}')
print(f'hex: {i:x}')


bin: 10011010010
oct: 2322
hex: 4d2

In [16]:
print(f'bin: {i:#b}')
print(f'oct: {i:#o}')
print(f'hex: {i:#x}')


bin: 0b10011010010
oct: 0o2322
hex: 0x4d2

In [17]:
f = 12.3456

In [18]:
print(f'digit(decimal): {f:.3f}')
print(f'digit(all)    : {f:.3g}')


digit(decimal): 12.346
digit(all)    : 12.3

In [19]:
print(f'exponen: {f:.3e}')


exponen: 1.235e+01

In [20]:
f = 0.123

print(f'percent: {f:.2%}')


percent: 12.30%

In [21]:
n = 123

In [22]:
print(f'{{}}-{n}-{{{n}}}')


{}-123-{123}

In [23]:
n = 123
i = 8

In [24]:
print('{n:0{i}}'.format(n=n, i=i))


00000123

In [25]:
print(f'{n:0{i}}')


00000123

In [26]:
f = 1.2345

for i in range(5):
    print(f'{f:.{i}f}')


1
1.2
1.23
1.234
1.2345

In [27]:
print('x\ty')


x	y

In [28]:
print(r'x\ty')


x\ty

In [29]:
x = 'XXX'
y = 'YYY'

In [30]:
print(f'{x}\t{y}')


XXX	YYY

In [31]:
print(rf'{x}\t{y}')


XXX\tYYY

In [32]:
print(fr'{x}\t{y}')


XXX\tYYY

In [33]:
a = 3
b = 4

In [34]:
# print('{a} + {b} = {a + b}'.format(a=a, b=b))
# KeyError: 'a + b'

In [35]:
print(f'{a} + {b} = {a + b}')


3 + 4 = 7

In [36]:
print(f'{a} * {b} = {a * b}')


3 * 4 = 12

In [37]:
print(f'{a} / {b} = {a / b:.2e}')


3 / 4 = 7.50e-01

In [38]:
d = {'key1': 3, 'key2': 4}

In [39]:
print('{0[key1]}, {0[key2]}'.format(d))


3, 4

In [40]:
# print('{0["key1"]}, {0["key2"]}'.format(d))
# KeyError: '"key1"'

In [41]:
print(f'{d["key1"]}, {d["key2"]}')


3, 4

In [42]:
# print(f'{d[key1]}, {d[key2]}')
# NameError: name 'key1' is not defined

In [43]:
# print(f'{d['key1']}, {d['key2']}')
# SyntaxError: invalid syntax

In [44]:
print(f"{d['key1']}, {d['key2']}")


3, 4

In [45]:
# print(f'{d[\'key1\']}, {d[\'key2\']}')
# SyntaxError: f-string expression part cannot include a backslash