= "Braithwaite I/O"
website
f"Welcome to {website}!"
'Welcome to Braithwaite I/O!'
Myles Braithwaite
November 2, 2018
f-strings are TOTALLY AWESOME!
{ }
dict
class
…import timeit
format_funcs = {
"f-strings": """
def format(superhero, rank):
return f'{superhero} has a rank of {rank}!'
""",
"%-formatting": """
def format(superhero, rank):
return '%s has a rank of %s!' % (superhero, str(rank))
""",
".format()": """
def format(superhero, rank):
return '{} has a rank of {}!'.format(superhero, str(rank))
""",
"concatenation +": """
def format(superhero, rank):
return superhero + ' has a rank of ' + str(rank) + '!'
""",
"concatenation ()": """
def format(superhero, rank):
return superhero, ' has a rank of ', str(rank), '!'
""",
}
test_func = """def test_format():
for superhero in ('Wonder Woman', 'Supergirl', 'Batman', 'Robin'):
for rank in range (1, 101):
format(superhero, rank)
"""
data = []
for key, func in format_funcs.items():
data.append(
{
"method": key,
"time": float(
timeit.timeit("test_format()", func + test_func, number=10000)
),
}
)
%matplotlib inline
import pandas as pd
df = pd.DataFrame(
[
{"method": "f-strings", "time": 1.2825216680000153},
{"method": "%-formatting", "time": 2.282235770999989},
{"method": ".format()", "time": 2.983771015000002},
{"method": "concatenation +", "time": 1.9718771689999812},
{"method": "concatenation ()", "time": 1.402194948999977},
]
)
Made by Myles Braithwaite with ❤️ in Toronto.