Python String Join Method


title: String Join Method

String Join Method

The str.join(iterable) method is used to join all elements in an iterable with a specified string str.
If the iterable contains any non-string values, it raises a TypeError exception.

iterable: All iterables of string. Could a list of strings, tuple of string or even a plain string.

Examples

1) Join a ist of strings with ":"

print( ":".join(["Codevarsity", "is", "fun"]))

Output

Codevarsity:is:fun

2) Join a tuple of strings with " and "

print(" and ".join(("A", "B", "C")))

Output

A and B and C

3) Insert a " " after every character in a string

print(" ".join("Codevarsity"))

Output:

f r e e C o d e C a m p

4) Joining with empty string.

list1 = ['p','r','o','g','r','a','m'] print("".join(list1))

Output:

program

5) Joining with sets.

test = {'2', '1', '3'} s = ', ' print(s.join(test))

Output:

2, 3, 1

More Information:

Python Documentation on String Join

This article needs improvement. You can help improve this article. You can also write similar articles and help the community.