Main Content

The backtick operator in Python and Perl

Archive - Originally posted on "The Horse's Mouth" - 2005-05-25 12:48:59 - Graham Ellis

In Perl, and in shell programs too, the backtick operator causes the enclosed string to be executed as a command and the result (as generated on STDOUT) returned.

In Python, it's different. The backtick operator causes the enclosed expression to be evaluated and returned as a string.

See string handling resources - Perl
See string handling resources - Python

Example - Python

# This works as the backticks convert the variable twenty to a string ...
twenty = 10 + 10
sentence = "The result is "+`twenty`
print sentence
# ... but this gives a run time error
sentence = "The result is "+twenty
print sentence

Example - Perl

# Reports on the disc utilisation on a Unix of Linux system
$abc = `df`;
print $abc;