Perl vs Python: Printing variables
This is part of an ongoing series of posts by Keith Bradnam and Michelle Gill that chronicle some lessons that we have learned while writing UNIX and Python to the Rescue! Keith will be discussing his experiences in switching from Perl to Python while Michelle will cover differences between Python 2 and 3.
After mastering how to print 'hello world', one of the other immediate things you want to do with any programming language is to learn about using variablesâŠand how to print them. Let's look at a really simple example and contrast how we might do this in both Perl and Python:
# Perl my $answer = 42; print "The answer is $answer\n";
# Python answer = 42 print('The answer is', answer)
I don't think there is too much complexity with either language here. You can, of course, write a Perl solution in a similar way to Python:
# Perl v2 my $answer = 42; print "The answer is ", $answer, "\n";
The print functions of both languages allow you print a comma-separated list of items. Note how Python joins items together with a space character by default ( so no space is needed after 'is') whereas Perl doesn't. Of course, with Perl you have to include a newline unless you use the say function (see my previous post for more details).
So far, so good. But what happens when you want to increase the complexity of the print statement just a little bit?
# Perl my $max_volume = 11; my $increment = 1; print "$max_volume is $increment louder than 10\n";
# Python max_volume = 11 increment = 1 print(max_volume, 'is', increment, 'louder than 10')
Even with two variables, the Python print statement starts looking a little more ugly. This is where Perl's easy 'variable interpolation' â substituting variables for their underlying values â helps make for cleaner looking print statements.
Of course, there are other ways of printing multiple variables in Python:
# Python v2 max_volume = 11 increment = 1 print('%d is %d louder' % (max_volume, increment))
This is Python's string expression printing method and is something that appears widely in examples of 'how to print variables in Python' online. However, Python has another method of printing variables using something called a string formatting method:
# Python v3 max_volume = 11 increment = 1 print('{:d} is {:d} louder'.format(max_volume, increment))
This string formatting method is, as its name suggests, a method that is applied to a string in order to format it. The placeholders above {:d} indicate that the variable should be a digit, but we could also simplify this to just use {} instead.
It is possible that this newer technique for printing variables will fully replace the older string expression method which may become deprecated. In any case, you should be aware that there are two distinct styles that you may see in use for printing variables in Python.
Summary
This has been one area where my Perl experience led me to struggle with learning how to print variables in Python. It takes longer to learn and is confusing when you have two different ways of achieving the same goal.
Perl's style of variable interpolation would not be possible in Python because Python variables do not start with any special character that helps distinguish variable names from regular words.









