How can I concatenate an argument and a string?

Question

How can I concatenate an argument and a string?

Answer

Arguments given to a function can be used like any other variable. Recall that we can concatenate a string and a variable like this: my_concatenated_string = "HOLY " + cow_string_var.
So all we need to do for this function is return parameter_name + "the string it wants us to concatenate with it!".

3 Likes

I am bit confused
Why its not
print string_function(s) and in the result there is no ā€˜sā€™ concatenated .

Can you explain this?

3 Likes

Note that s is written as a variable, not a string. We are given to assume that s has been given some assignment prior to this function call.

    def string_function(to_add):
        return "String to concatenate with... " + to_add

    s = "Add me to the string!"
    print (string_function(s))
    # String to concatenate with... Add me to the string!
5 Likes