Python

Python Split a String in Half

In Python, strings are a series of elements/items. The strings are unchangeable and are manipulated by various functions. Each element is stored in a specific place in strings. We utilize the indexes to approach the elements. The use of the slice () function is one of the ways to split a string. It decomposes the string and returns the parts of the string. Sometimes more than one string is returned by this function. This function utilized the indexing method for splitting. The string is divided into two halves by using this technique. This application can emerge in a variety of areas. Let us explain the specific ways with the examples to do this.

Use slice Notation

We can split the strings into two halves by using the slice () constructor. We separate the first half and second half of the string and then save these halves in different variables.

To execute the codes in Python language, first install spyder software version 5. Now, we create a new file by tapping “Ctrl+N” from the keyboard, which is named untitled.py3.

In this example, we take “travelling” as a string. The variable used for this string is ‘data’. The variable used for the len() function is ‘x’. Then we take two variables. In these two variables, we pass the slice () constructor.

For this constructor, we pass two arguments. Here, 2 is passed as a parameter of the constructor. It means we want two halves of the string. Similarly, for the second half of the string, we again divide the string length by 2. Then we apply a print statement to get the output of this code. Let’s run the code by tapping ‘F5’ from the keyboard.

The print statement prints the original string “travelling” into two halves “trave” “lling”. So after passing the slice () constructor the length of the string divides into two halves. The first five characters of the string “travelling” is separated from the last five characters of the string. By using the slice () method, the original string is divided in a half.

Use Split () Function

This function splits the string into smaller sections. This is the opposite of merging many strings into one. The split () function contains two parameters. In the first parameter, we pass the symbol that is used for the split. This symbol can be whitespace, comma, hyphen, or words.

Here, the string which we take for splitting is “I love to visit beautiful places”. As there are two arguments passed on this function. The first parameter is space. So in this code, space is used for the split. We don’t mention the second parameter, which represents the limit of splits. If the second parameter is mentioned in the code, the only maximum number of characters is returned.

In an output, all the elements of the original string are separated by a comma by using the split () function.

Use Newline (\n) Character

In Python, the string is split by the use of the newline (\n) character.  In this instance, we take a string “I \nlove \nto \nvisit \ndifferent \nplaces”. We apply (\n) character in between the words of the string.

Then, we call the print statement. This returns each word of the string starting with a new line. As we apply (\n) character to every element of the string. The next print statement prints the string with whitespaces in between the characters. Here we apply the split () function. Whitespace is passed as a parameter of the split () function.

Each word of the string is in a separate line. And by the second print statement, each element of the original string is separated by whitespaces.

Length of String is Even

In this instance, we take a string “visual” as a variable ‘data’. This string consists of even characters. We use the len() function which gives us the length of the given string. In string2, we again divide the length of the string by 2, to get to the second half of the string.

Here, we ‘//’ operator for dividing the length of the original string, and it returns an integer. If we have a string that has odd characters. Then, we get an extra letter. Because the odd number is not divided by 2, so we get an extra letter.

The first half consists of the first three characters of the original string “vis” and the second half contains the remaining three characters of the string “ual”.

Use If-Else Statement

If we have a string that has odd characters, then we get an extra letter in any one half of the string. We get two unequal strings as a result. Because the odd number is not divided by 2. So the resultant strings are not equal. If we want to remove an extra character, we can remove it. For this purpose, we apply the if-else condition here. We utilize the if-else statement to see whether the original string is even in length or not. The length of the original string is determined by using the len() function. If the length is an even number, divide that length by 2. And so it divides the string also. Else increment the half-length by one and it neglects the extra element.

In this code, the variable used for the string is ‘data’. And the string is “badminton”. Here the string has odd characters. There are two more variables for storing the halves of the string. The other variables are string1 and string2. We divided the length of the original string by 2. Here we apply the if-else condition.

If the length is an even number, divide that length by 2. And by this, the string is also divided. Else increment the half-length by one. After incrementing by one, we neglect the extra element. Because we want two halves of the string but the original string length is odd. So that the original string is not divided into two strings of equal characters. One half has an extra character and we remove that extra character.

In the output, the string “badminton” has odd characters. So it is not divided into two halves. The character ‘I’ is an extra character. So we removed this extra character to get the halves with equal characters. And now we have “badm” as a first-half and “nton” as the second half.

Conclusion

In this article, we describe the ways how to divide a string. We utilize the slice () function to split the elements of the string. By this function, the elements are separated by space or any symbol which we pass a parameter of the slice () function. We also split the items of the string by using the newline (\n) character. Any method can be used to get your work done.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content