Main Content
String duplication - x in Perl, * in Python and Ruby Archive - Originally posted on "The Horse's Mouth" - 2006-12-07 02:21:58 - Graham Ellis
In Python and Ruby you can duplicate a string by running the multiply operator (* ) on a string object, and in perl you can use the x operator. There have been many times when I've looked at this facility and said to myself "very nice, but isn't that a feature looking for a benefit" - in other words, something that's nice but not much use. Well ... there's always a few cases to disprove such a theory:
s/^(\s+)/" " x length()/e;
That's Perl - replace all leading spaces on a line with the same number of leading sequences.
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 This article 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 The backtick operator in Python and Perl R109 - Strings and Regular Expressions Clarrissa-Marybelle - too long to really fit? Regular Expressions for the petrified - in Ruby Global Regular Expression matching in Ruby (using scan) Ruby - standard operators are overloaded. Perl - they are not Ruby - a teaching example showing many of the language features in short but useful program Matching regular expressions, and substitutions, in Ruby Divide 10000 by 17. Do you get 588.235294117647, 588.24 or 588? - Ruby and PHP Ruby - examples of regular expressions, inheritance and polymorphism Object Oriented Ruby - new examples Ruby collections and strings - some new examples Neatly formatting results into a table Search and replace in Ruby - Ruby Regular Expressions The dog is not in trouble Regular Expressions in Ruby Ruby to access web services Ruby Programming Course - Saturday and Sunday What are exceptions - Python based answer String interpretation in Ruby Regular expressions made easy - building from components Regular Express Primer Ruby v Perl - interpollating variables puts - opposite of chomp in Ruby 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 This article The backtick operator in Python and Perl x operator in Perl Here documents