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;
Some other articles
Y108 - String Handling Prining a pound sign from Python AND running from the command line at the same time Python formatting update - including named completions Command line parameter handling in Python via the argparse module Python - comparison of old and new string formatters Identifying and clearing denial of service attacks on your Apache server Formatting options in Python Why are bus fares so high? Collections in Python - list tuple dict and string. Formatting output - why we need to, and first Python example Backquote, backtic, str and repr in Python - conversion object to string Teaching dilemma - old tricks and techniques, or recent enhancements? Python string formatting - the move from % to str.format Formatting output in Python through str.format Matching a license plate or product code - Regular Expressions Matching to a string - what if it matches in many possible ways? Python - splitting and joining strings Formatted Printing in Python Running operating system commands from your Python program Regular Expressions in Python Flexible search and replace in Python Pound Sign in Python Program Strings as collections in Python Python Regular Expressions Underlining in Perl and Python - the x and * operator in use Python - formatting objects Regular Express Primer Python - two different splits String duplication - x in Perl, * in Python and Ruby Splitting Pythons in Bradford Matching within multiline strings, and ignoring case in regular expressions Pieces of Python Breaking bread The fencepost problem Python printf Splitting the difference This article P205 - Initial String Handling Sample answers to training course exercises - available on our web site Dark mornings, dog update, and Python and Lua courses before Christmas Using Perl to generate multiple reports from a HUGE file, efficiently Single and double quotes strings in Perl - what is the difference? Lots of ways of doing it in Perl - printing out answers Removing the new line with chop or chomp in Perl - what is the difference? Are you learning Perl? Some more examples for you! Intelligent Matching in Perl Perl - skip the classics and use regular expressions Seven new intermediate Perl examples String matching in Perl with Regular Expressions Underlining in Perl and Python - the x and * operator in use Regular Express Primer Ruby v Perl - interpollating variables String duplication - x in Perl, * in Python and Ruby This article x operator in Perl Here documents