MulaiMulai sekarang secara gratis

Adding arbitrary arguments

Python allows custom functions to accept any number of positional arguments through the use of "Arbitrary arguments". This flexibility enables functions to be used in a variety of ways while still producing the expected results.

Using this power, you'll build a function that concatenates (joins together) strings, regardless of how many blocks of text are provided!

Latihan ini adalah bagian dari kursus

Intermediate Python for Developers

Lihat Kursus

Petunjuk latihan

  • Define a function called concat() that accepts arbitrary arguments called args.
  • Use a for loop to iterate over each arg in args.
  • Call the function to test that it works correctly.

Latihan interaktif praktis

Cobalah latihan ini dengan menyelesaikan kode contoh berikut.

# Define a function called concat
def ____(____):
  """Concatenates multiple string arguments with spaces between them."""

  result = ""

  # Iterate over the Python args tuple
  for ____ in ____:
    result += " " + arg
  return result

# Call the function
print(____("Python", "is", "great!"))
Edit dan Jalankan Kode