Python print with multiple arguments vs printing an fstring

Viewed 876

Is there an unspoken rule, or a PEP rule that says that it's better to use an f string when printing something to the screen rather than using print with multiple arguments? Example:

age = 80
name = "John"
print(f'My name is {name} and I am {age} years old')
print('My name is', name, 'and I am', age, 'years old')

I was trying to explain to someone that f-string are more used, but he kept insisting that the second option is easier. Both examples will work, I am just curious if there is a rule that suggests using f strings, or why is it bad practice to use print with multiple arguments.

Thanks

2 Answers

In the presented example there is no real difference. However, while working with strings you sometimes need to create a "parametric" string: in such a case you will be obliged to use f-string, format() method or the old-style string formatting. Which one to use if mostly depend on your preferences and your application (for example, sometimes you are blocked with an older version of Python).

These will work:

name = 'Johnny'
age = 18

# old-style
s1 = 'My name is %s and I am %s years old' % (name, age)

# format()
s2 = 'My name is {0} and I am {1} years old'.format(name, age)

# f-string
s3 = f'My name is {name} and I am {age} years old'

You can see it as an evolution s1 --> s2 --> s3 (so more modern - more developed)

While these would not work as expected

# This will give a tuple
s4 = 'My name is', name, 'and I am', age, 'years old'

# This will throw an exception as the age is of the `int` type
s5 = 'My name is' + name + 'and I am' + age + 'years old'

We could also add that some nice features are accessible with string formatting (s1, s2 and s3) and not accessible with print(a, b, c).

I should preface this by saying that I personally prefer using f-strings, and therefore my answer might be biased towards using it. However, I think there is at least one clear advantage of using f-strings over the multiple arguments, which is that f-strings are strings.

This might seem like an obvious observation, but it means that any manipulation operation that I perform with f-strings can be directly translated into a singular statement outside of print() as well. The second approach, however, utilizes multiple arguments, which is not something that I can package into a separate statement. IMO, this makes refactoring outputs much easier, since you can simply copy+paste code from print statements to somewhere else and maintain consistency in your code styling.

Another point is the performance of f-strings. While I haven't seen explicit comparisons to multi-argument print()s, there is a bunch of posts comparing f-strings to the other methods (e.g., here [https://realpython.com/python-f-strings/#speed]). In a quick experiment I did myself with timeit, i found the following:

import timeit
y = David
x = Dan
timeit.timeit("print(f'My name is {y} {x}')", setup="from __main__ import x,y", number=1000000)
# 4.74 s

timeit.timeit("print('My name is', y, x)", setup="from __main__ import x,y", number=1000000)
# 5.72 s

Of course, speed for print statements is usually not so crucial that it matters, but might just be another argument.

Related