Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 8041

Python • Re: Problem with print in a for loop.

$
0
0
It's personal preference.
Though I believe the f-string method is now faster than the older .format methods.
f-strings are a 1ns faster on my system:

Code:

[deadeye@nexus ~]$ ipythonPython 3.14.2 (main, Dec  7 2025, 02:20:36) [GCC 15.2.1 20251112]Type 'copyright', 'credits' or 'license' for more informationIPython 9.8.0 -- An enhanced Interactive Python. Type '?' for help.Tip: Use `%timeit` or `%%timeit`, and the  `-r`, `-n`, and `-o` options to easily profile your code.In [1]: name = "World"In [2]: %timeit f"Hello {name}"36.8 ns ± 0.328 ns per loop (mean ± std. dev. of 7 runs, 10,000,000 loops each)In [3]: %timeit "Hello {}".format(name)104 ns ± 1.31 ns per loop (mean ± std. dev. of 7 runs, 10,000,000 loops each)
Speed is not an argument to use it or not to use it.

Statistics: Posted by DeaD_EyE — Tue Dec 16, 2025 3:31 pm



Viewing all articles
Browse latest Browse all 8041

Trending Articles