Python join array to string

The string join[] method returns a string by joining all the elements of an iterable [list, string, tuple], separated by the given separator.

Example

text = ['Python', 'is', 'a', 'fun', 'programming', 'language']

# join elements of text with space print[' '.join[text]]

# Output: Python is a fun programming language

Syntax of String join[]

The syntax of the join[] method is:

string.join[iterable]

join[] Parameters

The join[] method takes an iterable [objects capable of returning its members one at a time] as its parameter.

Some of the example of iterables are:

  • Native data types - List, Tuple, String, Dictionary and Set.
  • File objects and objects you define with an __iter__[] or __getitem[]__ method.

Note: The join[] method provides a flexible way to create strings from iterable objects. It joins each element of an iterable [such as list, string, and tuple] by a string separator [the string on which the join[] method is called] and returns the concatenated string.

Return Value from join[]

The join[] method returns a string created by joining the elements of an iterable by the given string separator.

If the iterable contains any non-string values, it raises the TypeError exception.

Example 1: Working of the join[] method

# .join[] with lists
numList = ['1', '2', '3', '4']
separator = ', '

print[separator.join[numList]]

# .join[] with tuples numTuple = ['1', '2', '3', '4']

print[separator.join[numTuple]]

s1 = 'abc' s2 = '123' # each element of s2 is separated by s1 # '1'+ 'abc'+ '2'+ 'abc'+ '3'

print['s1.join[s2]:', s1.join[s2]]

# each element of s1 is separated by s2 # 'a'+ '123'+ 'b'+ '123'+ 'b'

print['s2.join[s1]:', s2.join[s1]]

Output

1, 2, 3, 4
1, 2, 3, 4
s1.join[s2]: 1abc2abc3
s2.join[s1]: a123b123c

Example 2: The join[] method with sets

# .join[] with sets
test = {'2', '1', '3'}
s = ', '

print[s.join[test]]

test = {'Python', 'Java', 'Ruby'} s = '->->'

print[s.join[test]]

Output

2, 3, 1
Python->->Ruby->->Java

Note: A set is an unordered collection of items, so you may get different output [order is random].

Example 3: The join[] method with dictionaries

# .join[] with dictionaries
test = {'mat': 1, 'that': 2}
s = '->'

# joins the keys only

print[s.join[test]]

test = {1: 'mat', 2: 'that'} s = ', ' # this gives error since key isn't string

print[s.join[test]]

Output

mat->that
Traceback [most recent call last]:
  File "...", line 12, in 
TypeError: sequence item 0: expected str instance, int found

The join[] method tries to join the keys [not values] of the dictionary with the string separator.

Note: If the key of the string is not a string, it raises the TypeError exception.

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    join[] is an inbuilt string function in Python used to join elements of the sequence separated by a string separator. This function joins elements of a sequence and makes it a string. 

    Syntax: string_name.join[iterable] 

    Parameters: 

    • Iterable – objects capable of returning their members one at a time. Some examples are List, Tuple, String, Dictionary, and Set

    Return Value: The join[] method returns a string concatenated with the elements of iterable. 

    Type Error: If the iterable contains any non-string values, it raises a TypeError exception. 

    Example 1: Joining with an empty string

    Here, we join the list of elements using the join method.

    Python3

    list1 = ['g', 'e', 'e', 'k', 's']

    print["".join[list1]]

    list1 = " geeks "

    print["$".join[list1]]

    Output: 

    geeks
    $g$e$e$k$s$ 

    Example 2: Joining String with lists using join[]

    Here, we join the tuples of elements using the join[] method in which we can put any character to join with a string.

    Python3

    list1 = ['1', '2', '3', '4']

    s = "-"

    s = s.join[list1]

    print[s]

    Output: 

    1-2-3-4

    Example 3: Joining String with sets using join[]

    In this example, we are using a Python set to join the string.

    Note: Set contains only unique value therefore out of two 4 one 4 is printed.

    Python3

    list1 = {'1', '2', '3', '4', '4'}

    s = "-#-"

    s = s.join[list1]

    print[s]

    Output: 

    1-#-3-#-2-#-4

    Example 4: Joining String with a dictionary using join[]

    When joining a string with a dictionary, it will join with the keys of a Python dictionary, not with values.

    Python3

    dic = {'Geek': 1, 'For': 2, 'Geeks': 3}

    string = '_'.join[dic]

    print[string]

    Output:

    'Geek_For_Geeks'

    How do you join an array of strings in Python?

    Note: The join[] method provides a flexible way to create strings from iterable objects. It joins each element of an iterable [such as list, string, and tuple] by a string separator [the string on which the join[] method is called] and returns the concatenated string.

    What is join [] in Python?

    join[] is an inbuilt string function in Python used to join elements of the sequence separated by a string separator. This function joins elements of a sequence and makes it a string. Syntax: string_name.join[iterable]

    How do you join a list element into a string?

    If you want to concatenate a list of numbers [ int or float ] into a single string, apply the str[] function to each element in the list comprehension to convert numbers to strings, then concatenate them with join[] .

    How do you join a list of objects in Python?

    Use list comprehension and str..
    class Obj:.
    def __str__[self]:.
    return 'I am an object! '.
    object_list = [Obj[], Obj[], Obj[]].
    object_string = ', '. join[[str[x] for x in object_list]].
    print[object_string].

    Chủ Đề