Last Updated:

Split Python method

The Python programming language involves the use of various types of information data. Among them, you can distinguish the main types that every programmer will have to deal with regularly. Examples are variations of string, ingteger, float. Today we're going to look at split and try to define what it is.

Split Python method

Description of the methodology

Let's say you want to enter a string of characters and want list items to be entered without wrapping them to a new line. You also want all these elements to be separated from each other by means of spaces. In this case, a function called input comes to the rescue.

Fact! The Input function is actually a string in this case.


As soon as you have applied the above function, you can resort to a string method called split. What is it for? It is capable of returning a list of strings. He does this so that the original line is as if cut. In fact, its individual parts are highlighted, while using spaces. Here's an example:


A = input().split()

Have you launched such a program? Try entering the combination 123 immediately after that. As a result, you will get a list that will be mapped to the value ['1', '2', '3'].

Note! Rows will appear in the list structure. That is, numbers will not be detected in it.

If you want to create a list that consisted only of numbers, you can convert these elements one number at a time. Here's what it looks like in practice:

for i in range(len(A)):

A[i] = int(A[i])

The same can be done in one line, if you use special functions. Such options are implied, which are called map and list. Let's find out what it might look like:

for i in range(len(A)):

A[i] = int(A[i])

You can do all the same thing using the above elements, but only in line 1:

A = list(map(int, input().split()))

If you have to interact with a list of valid numbers, then you can make a replacement in this list. It will concern the int component, which will later be replaced by the float variety.

The technique mentioned earlier has 1 optional parameter. It directly affects which string should be used when separating the components of the list, that is, the list. Consider the split('.') variety as an example. What can such an object do? In this case, it returns the list that was cut with the program to the characters of the original string '.'.

The use of opposite methods allows you to get a list using a single-line command. In such a situation, the string method, which is called Join, will come to the rescue. The list of strings is in this case the only parameter for this technique. What do we get in the end, as a result?

A string is generated that is generated by connecting list components. They act as a parameter. They are connected in a single line. At the same time, you can see a separator between the components of the list that corresponds to the line where the components in question are applicable. Let's consider as an example the program:

A = [‘red’, ‘green’, ‘blue’]

print(‘ ‘.join(A))



Here you can display special lines that will take the form 'red green blue', redgreenblue and red***green***blue.

But it also happens that there are numbers in the list and it consists of them. Then you will need the map function. It will be required on an additional basis. Accordingly, separating them with spaces, the components of the list can be represented in this version:

print(‘ ‘.join(map(str, A)))

Other similar techniques

There are methods called find and replace. The first of these is a tool for finding a substring in a string. In addition, the specified object is able to return the index of the first component of the subtune that was found. If it is not found, then a return to the indicator -1 will be ensured. Let's look at an example:

>>> s

‘red blue orange white’

>>> s.find(‘blue’)


>>> s.find(‘green’)


You can search not on the entire line, but on a certain segment of it. So you get the index of the segment, which will be considered its final. If the finish line is not found, the search is performed until the end of the line can be reached:

>>> letters = ‘ABCDACFDA’

>>> letters.find(‘A’, 3)


>>> letters.find(‘DA’, 0, 6)


Subsequently, the search continues, but from the third index and up to the final indicators. It is also produced from the first day and up to the sixth component of the list. It is important to note that the find method is responsible for returning only the object that is ranked as the first in a row. Suppose the last letter A is not found using letters.find('A', 3).

This is because this letter is found under an index that has been designated as 4. The method, which is called replace(), is distinguished by the ability to adapt a single line to any other. For clarity, here is an example:

>>> letters.replace(‘DA’, ‘NET’)


Despite the opposition, the original line is not subject to any changes, respectively, the result will be as follows:

>>> letters


It turns out that if you need to save the result, you will have to assign it one or another variable. Here's what the result will be:

>>> new_letters = letters.replace(‘DA’, ‘NET’)

>>> new_letters


So, we have studied the fundamental rules and features that are characteristic of the method by which you can work with strings. It is necessary to understand the characteristics and rules of working with such tools in order to successfully master programming skills and start creating your own programs. Illustrative examples will help you understand this process even better and delve into all the nuances without much difficulty.