split() and join() function in Python

Question

Explain split() and join() functions in Python?

in progress 1
Pragya Rani 55 years 9 Answers 2238 views Contributor 0

Answers ( 9 )

  1. split function splits the string based on a delimiter . for eg ‘hi i am awesome’.split() splits the string based on single space and outputs an array of strings containing individual words.
    join does the opposite ”.join(array) joins together an array of strings and returns in string form

  2. we can use the function split() to split a string and join() to join a string.

    1) split() function
    split() method returns a list of strings after breaking the given string by the specified separator.
    If it is not provided, then any white space is a separator.

    syntax:
    str.split(separator,max-split)
    separator – This is is a delimiter. The string splits at the specified separator.
    max-split – This provides the maximum no of splits to make. If no value is provided, there is no limit.

    code:
    text = “This-is-Data-Monk”
    word = text.split(‘-‘)
    print(word)

    output:
    [‘This’,’is’,’Data’,’Monk’]

    2) Join() function
    The join method returns a string in which the elements of sequence have been joined by a separator.

    syntax : string_name.join(iterable)

    The join() method takes iterable – objects capable of returning its members one at a time.
    Some examples are List, Tuple, String, Dictionary and Set

    code :
    a = [‘D’,’A’,’T’,’A’,’M’,’O’,’N’,’K’]
    word = ‘-‘.join(a)
    print(word)

    output:
    D-A-T-A-M-O-N-K

  3. Split() and Join() are string functions in python that works in contrast to each other.

    1. Split() – It is used to split a string into a list of words that were separated by a given separator.
    Syntax : string_to_be_split.(separator,max_split)
    Lets see some examples to learn more.
    string = ‘ Hi ! there’
    Example 1 –
    string.split() # By default , whitespace is string separator and max_split = -1 that means return all occurences
    Output : [‘Hi’, ‘!’ , ‘there’]
    Example 2 –
    string.split(‘!’)
    Output : [‘Hi ‘, ‘ there’] # notice whitespace with output
    Example 3 –
    string.split(max_split=1) # return up to index value 1 ie 2 occurences
    Output : [‘Hi’, ‘!’ ]

    2. Join() – 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.
    Syntax : string_separator,join(iterable)
    list1 =[‘A’,’B’,’C’,’D’]
    Example 1-
    “”.join(list1)
    Output: ABCD

    Example 2-
    s=” – ”
    s.join(list1)
    Output : A – B – C – D

  4. Split() – The split function is basically used in a string to return a list.
    Join() – The join function is applied on list of elements and it returns a string

  5. Split() splits the string at the specified separator, and returns a list.
    Code :
    a = “Hello , World”
    print(a.split(“,”))
    Output – ‘Hello’ , ‘World’

    Join() joins the elements of an iterable to the end of the string.

  6. 1) split(): It is used to split the string based on a delimiter.
    Eg1: Split sentence in words:
    >>> mary = ‘Mary had a little lamb’
    >>> mary.split()
    [‘Mary’, ‘had’, ‘a’, ‘little’, ‘lamb’]
    Eg2: Splitting on a Specific Substring
    >>> mary = ‘Mary had a little lamb’
    >>> mary.split(‘a’) # splits on ‘a’
    [‘M’, ‘ry h’, ‘d ‘, ‘ little l’, ‘mb’]

    2) join(): 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.
    Eg1: Create a sentence (string) from list of words
    >>> mwords
    [‘Mary’, ‘had’, ‘a’, ‘little’, ‘lamb’]
    >>> ‘ ‘.join(mwords)
    ‘Mary had a little lamb’
    Eg2: Join on a separator
    >>> ‘–‘.join(mwords)
    ‘Mary–had–a–little–lamb’
    >>> ‘t’.join(mwords)
    ‘Marythadtatlittletlamb’
    >>> print(‘t’.join(mwords))
    Mary had a little lamb

  7. 1) split(): It is used to split the string based on a delimiter.
    Eg1: Split sentence in words:
    mary = ‘Mary had a little lamb’
    mary.split()
    [‘Mary’, ‘had’, ‘a’, ‘little’, ‘lamb’]
    Eg2: Splitting on a Specific Substring
    mary = ‘Mary had a little lamb’
    mary.split(‘a’) # splits on ‘a’
    [‘M’, ‘ry h’, ‘d ‘, ‘ little l’, ‘mb’]

    2) join(): 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.
    Eg1: Create a sentence (string) from list of words
    mwords
    [‘Mary’, ‘had’, ‘a’, ‘little’, ‘lamb’]
    ‘ ‘.join(mwords)
    ‘Mary had a little lamb’
    Eg2: Join on a separator
    ‘–‘.join(mwords)
    ‘Mary–had–a–little–lamb’
    ‘t’.join(mwords)
    ‘Marythadtatlittletlamb’
    print(‘t’.join(mwords))
    Mary had a little lamb

  8. As the name suggests, the split() function is used to split a string into words based on some parameters. And the join() function is used to join the words to form a string.

    Both the functions have reverse functionality to each other.

    split() –
    a.split(‘,’) — this function will split the string ‘a’ into words at ‘,’. It will return a list of those words. ‘,’ is a delimiter. Delimiters are used to determine the point at which the function has to split. We can choose any character for the delimiter.
    Examples:-
    1. ‘a,b,c,d,e’.split(‘,’) = [‘a’,’b’,’c’,’d’,’e’]
    2. ‘python is a programming language’.split(‘ ‘) = [‘python’, ‘is’, ‘a’, ‘programming’, ‘language’]

    join() –
    join() is exactly a reverse function of above. It joins elements (in the same sequence) present in a list/tuple by placing a parameter provided by the user between those elements. That parameter can be any character
    Examples:-
    1. ‘,’.join([‘a’,’b’,’c’,’d’,’e’])= ‘a,b,c,d,e’
    2. ‘ ‘.join([‘python’, ‘is’, ‘a’, ‘programming’, ‘language’]) = ‘python is a programming language’

  9. The split() method splits a string into a list.
    You can specify the separator, default separator is any whitespace.
    Syntax: string.split(separator, maxsplit)
    For example:
    txt = “hello, my name is Peter, I am 26 years old”
    x = txt.split(“, “)
    print(x)
    Output: [‘hello’, ‘my name is Peter’, ‘I am 26 years old’]

    The join() method takes all items in an iterable and joins them into one string.
    A string must be specified as the separator.
    Syntax: string.join(iterable)
    For example:
    list1 = [‘s’,’h’,’u’,’b’,’h’]
    print(“”.join(list1))
    Output: shubh

Leave an answer

Browse
Browse