thehammerons
thehammerons

Reputation: 55

How does the `join` operator work for string concatenation?

How can I properly use this operator to concatenate two strings?

string = "Hello"

a = " ".join(string, "World")

print(a)

Output ~ TypeError: join() takes exactly one argument (2 given)

Upvotes: 1

Views: 65

Answers (2)

Akshay Jain
Akshay Jain

Reputation: 81

The argument to join is basically an iterable, so you just need to put your strings as a list

You can do something like:

" ".join([string," World"])

Upvotes: 2

Gianluca Micchi
Gianluca Micchi

Reputation: 1653

You should call join on a list of strings.

string = "Hello"

a = " ".join([string, "World"])

print(a)

Upvotes: 1

Related Questions