What is the opposite of split in python?

In Python you can split a string with the split() method. It breaks up a string (based on the given separator) and returns a list of strings.

To split a string, we use the method .split(). This method will return one or more new strings. All substrings are returned in a newly created list.

Note: Split with the method str.split(sep, maxsplit). The 2nd parameter is optional. If maxsplit is set, it returns no more than maxsplit elements.

Related course: Complete Python Programming Course & Exercises

Introduction

The split function breaks a string down into smaller chunks (a list of strings). Splitting a string is the opposite of concatenation (merges multiple strings into one string).

The split() method takes maximum of 2 parameters, of which one is optional. The syntax of the split() function is:

str.split([separator [, maxsplit]])

The first parameter seperator is the string to split on, this could be a space, a comma, a dash, a word etcetera.

The second parameter maxsplit is the maximum number of splits. By default there is no maximum and most programs don’t require a maximum number of splits.

String split example

We can split a string based on a character " ", this is named the seperator.

s = "To convert the result to"
parts = s.split(" ")
print(parts)

Result:

Any character (seperator) can be used. If you want to get sentences you could use:

s = "Python string example. We split it using the dot character."
parts = s.split(".")
print(parts)

This will result in: ['Python string example', ' We split it using the dot character', '']

It’s not limited to a space, you can seperate on a comma:

>>> s = "alice,allison,chicago,usa,accountant"
>>> s = s.split(",")
>>> s
['alice', 'allison', 'chicago', 'usa', 'accountant']
>>>

You can split on a dot (turning texts into individual sentences)

>>> s = "This is an example sentence. It has several words, and is in the English language. It's dummy data for Python."
>>> s = s.split(".")
>>> s
['This is an example sentence', ' It has several words, and is in the English language', " It's dummy data for Python", '']
>>>

The seperator can be a word instead of a single symbol or character

s = s.split("is")
s = s.split("python")

Note that we overwrite s, when calling the s.split() function. This is optional, if you want to keep the string you can store the result in a new variable.
Something like words = s.split()

If no separator is defined if you call the split() function, it will use a whitespace by default.

>>> s = "hello world how are you"
>>> print(s.split())
['hello', 'world', 'how', 'are', 'you']
>>>

Related course: Complete Python Programming Course & Exercises

Introduction to Python Split String

In Python, strings can be defined as an array of characters represented by Unicode character. As Python doesn’t support char data type, Python has a single character as also a string. Strings are declared either in the single quote or double-quotes. The most import function on the string is the split() method. The split () function is a function when applied to any string it returns the list of strings after recognizing the delimiter and breaks the string into substrings. This method is used to split the strings into characters. Sometimes it is required to split large strings into small chunks of characters or substring. Spit () functions work the opposite of concatenation. As the split function returns the list of strings, it can be accessed using an indexing method in which every string or character of the given string is given the index number starting with number 0 and so on. In this topic, we are going to learn about Python Split String.

How Does Python split function work?

In python, the split () function is a function to split strings using separator white space being a default separator. As we know in Python, we have a concatenation of strings which means combining strings, whereas split function is the opposite of concatenation. Therefore Python uses a built-in function called split(), which breaks down the strings into other strings.

Whenever we split strings in Python, using the split() function will always be converted into lists. In Python, data types are not declared before any variable; hence, we need to assign it to some variable whenever the split() function is used. It will then be easily accessible using advanced for a loop. So when a string assigned to any variable and after applying a split() function to that variable, it will be converted to an array or list, and this array of strings can be accessed using an index.

Syntax:

Str.split(separator, maxsplit)

The split () method takes two parameters, as shown in the syntax above separator and maxsplit.

The separator parameter is optional because, by default, the whitespace is taken as a separator if not specified.

Maxsplit is also an optional parameter of the split () function; this parameter defines the maximum number of splits on the given string. By default, it will take -1 as the value of maxsplit if not specified.

Examples of Python Split String

Here are the following examples are mention below:

Example #1

The below code shows the simple program which uses a split() function without the maxsplit parameter.

Code:

str = 'Python split strings article' print(str.split())

Output:

The above program uses a split function where the parameter isn’t specified, so by default, it will split the sentence given in str with space as the delimiter.

Now let us see the example for the code which uses a split function with maxsplit parameter also.

Code:

items = 'Cheese, Curd, Butter, Milk ' print(items.split(', ', 2)) print(items.split(', ', 1))

Output:

In the above program, we have specified the maxsplit parameter as 2 and 1in the program. The items get split into 3 strings and 2 strings wherefrom the above it takes Cheese as 0th the string, Curd as 1st string, Butter and Milk as 2nd string.

There is another function, reverse split, which is defined as rsplit() in Python, which returns the list of string in reverse order, which means it starts from the end of the string and returns a string in a reverse way with the specified delimiter.

Syntax:

Text.rsplit (delimiter, maxsplit)

The syntax is similar to the split() function and the working also, but it returns the strings in reverse order with a specified separator.

If no argument is passed to the rsplit() function, then it returns the same as the split() function.

Example #2

Code:

txt = "apple, pineapple, custardapple" print(txt.rsplit())

Output:

Using the rsplit function with maxsplit argument. This will spit the string in a set of string from the right side of the given string.

Example #3

Code:

txt = "apple, pineapple, custardapple" print(txt.rsplit(',', 1))

Output:

The above screenshot is for the above program with maxsplit argument as 1, which will split the string from back to front and it returns two strings as shown it will return first ‘custardapple’ then it takes a second string as ‘apple, pineapple’.

Another example of the rsplit() function which splits at the letter t and the provided maxsplit is 1, so it will split the string into two strings first, it splits at t so ’rings’ is one string, and the second string will be ‘Python, split, s’ is another string. The code is given below with output and screenshot.

Example #4

Code:

txt = 'Python, split, strings' print(txt.rsplit('t', 1))

Output:

There is another function similar to a split() function known as splitfields(). The earlier split() function was used with only one parameter, and the splitfields() function used two parameters. Now split() also uses two parameters; if no argument, it will take whitespace as default delimiter and splits the string and returns the string list.

Conclusion

In Python, strings can be broken and accessed using a split() function, which splits the given string according to the specified separator or by default separator as whitespace. This function returns the array of strings, so as earlier in Python array can be accessed using indexing. Similarly, these sets of strings returned after the split() function can be accessed using indexing. In this, each of string or character is given a corresponding index number starting with 0 and can be accessed using this index number.

Recommended Articles

This is a guide to Python Split String. Here we discuss how does Python split function work along with the examples and appropriate syntax.  You may also have a look at the following articles to learn more –

  1. Python Slice String
  2. Python Find String
  3. Python Switch Case
  4. Python Threadpool

What is the opposite of split () in Python?

The inverse of the split method is join . You choose a desired separator string, (often called the glue) and join the list with the glue between each of the elements. The list that you glue together ( wds in this example) is not modified.

Which function does the reverse of split?

The split() method splits a String object into an array of string by separating the string into sub strings. The reverse() method reverses an array in place. The first array element becomes the last and the last becomes the first. The join() method joins all elements of an array into a string.

How do you reverse a split string?

Example 2: Reverse a String Using built-in Methods First, the string is split into individual array elements using the split() method. str. split("") gives ["h", "e", "l", "l", "o"] . The string elements are reversed using the reverse() method.

What is split and join in Python?

We use split to get data from CSV and join to write data to CSV. In Python, we can use the function split() to split a string and join() to join a string. For detailed article on split() and join() functions, refer these : split() in Python and join() in Python.

Postingan terbaru

LIHAT SEMUA