24

This week in Programming Language appreciation: the multiplication operator in Python. You can use it on strings and arrays for incredible ease of use. For a horizontal line in a terminal?

print("-" * 30)

Very helpful, and I have used it a lot. And I miss it in other languages.

Comments
  • 3
    I might do this weekly. You can discuss things you like about your favorite (or any) language, and I may appreciate them in a post!
  • 9
    I would rather use a string repeat function for that tbh. This kind of thing is what ticks me off about Python et. al. (don't get me wrong, I love Python otherwise).
  • 3
    @RememberMe I love syntax that reads like english. That's the largest reason Lua is my favorite language. It has many flaws otherwise.

    But seeing "-" * 30 and getting "------------------------------" as output just is so intuitive. That's exactly what you expect. And with Python's lack of type coercion, it's not confusing or ambiguous, either
  • 9
    So “3” * 3 is “333”? Amazing, what could go wrong
  • 1
    I like Python's for-else loops.
  • 4
    You could do that in many (statically typed) functional languages too, you just need to overload the * operator.

    (REPL link: https://bit.ly/33kz3PL)
  • 4
    To add on to @finiteAutomaton’s comment here’s an overload for c++:

    std::string operator*(std::string s, int n) {
    std::string out = "";
    while(n--) {
    out += s;
    }
    return out;
    }
  • 1
    Fuck Im gonna implement this in Java.
    It's something I never knew I needed
  • 2
    @Ranchu Java? Does Java have operator overloading now?
  • 1
    Funny, this is one of the things they borrowed from Perl. Only there it is a "x" operator.
    One of the uses is repeated patterns like the wake on LAN Magic packet.
  • 1
    @AlgoRythm sadly No, i'll Just smack it into a method.
  • 0
    That also works in ruby. Quite useful for debugging.
  • 1
    @AlgoRythm @Ranchu I believe Kotlin has it though...
  • 1
    @RagingCodeChimp Kotlin is the big tits rich sister to Java. Of course it does...
  • 1
    @AlgoRythm ok, I lol'ed 😂
Add Comment