Arbitrary Argument Lists, String Concatenation, and the Join Function in Python

What is the output of this code snippet?

 

def concat(*args, sep="/"):
    return sep.join(args)

print(concat("A", "B", "C", sep=","))

 

String concatenation is the process of creating a string by appending string arguments. The given function takes an arbitrary number of string arguments as specified by the *args keyword. The parameter sep declares the separator string to clue together two strings. The separator argument is a keyword argument because of the *args argument can have an arbitrary number of arguments. The keyword argument helps to differentiate whether the last parameter is part of *args or the sep argument.

The function concatenation is a wrapper for the join function to concatenate strings. The join function is defined in the string object sep. It concatenates an arbitrary number of strings using the separator to clue them together. Both functions achieve the same thing, but the first may be more convenient because the separator is a normal argument.
Yet, you will find yourself using the join function on a regular basis without writing your own wrapper functions. So you can as well learn its proper use now.


Are you a master coder?
Test your skills now!

Related Video

Solution

A,B,C

 

Leave a Comment

Your email address will not be published. Required fields are marked *