Is there another method to combine strings other than the concatenation operation?


#1

Question

In this exercise, the strings for the first and last names are joined using the + operator. Can this be done in another way?

Answer

Yes, string formatting would be another option which could be used to construct a string. The following code example shows a method to produce the same result as the + operator using Python’s string formatting. A new string is constructed using string formatting and is then returned from the function.

def introduction2(first_name, last_name):
  return "{last}, {first} {last}".format(first=first_name,last=last_name)