-
-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathhow_to_convert_an_integer_to_a_string.py
More file actions
58 lines (43 loc) · 1.27 KB
/
how_to_convert_an_integer_to_a_string.py
File metadata and controls
58 lines (43 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
"""
Tests the performance of all of the solutions listed in the following article:
https://therenegadecoder.com/code/how-to-convert-an-integer-to-a-string-in-python/
"""
from test_bench import test_bench
def control(_):
"""
Provides a control scenario for testing. In this case, none of the functions
share any overhead, so this function is empty.
:param _: a placeholder for the int input
:return: None
"""
pass
def convert_by_type_casting(integer: int) -> str:
"""
Converts an integer to a string by type casting.
:param integer: an integer
:return: the integer as a string
"""
return str(integer)
def convert_by_f_string(integer: int) -> str:
"""
Converts an integer to a string using f-strings.
:param integer: an integer
:return: the integer as a string
"""
return f"{integer}"
def convert_by_interpolation(integer: int) -> str:
"""
Converts an integer to a string using string interpolation.
:param integer: an integer
:return: the integer as a string
"""
return "%s" % integer
if __name__ == '__main__':
test_bench(
{
"Zero": [0],
"Single Digit": [5],
"Small Number": [1107321],
"Massive Number": [2 ** 64]
}
)