Open In App

Python | Split given string into equal halves

Last Updated : 03 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Sometimes, we need to simply divide the string into two equal halves. This type of application can occur in various domain ranging from simple programming to web development. Let’s discuss certain ways in which this can be performed. 

Method #1 : Using list comprehension + String slicing This is the naive method to perform this particular task. In this we just use brute divisions and slicing to separate first and last part of string. 

Python3




# Python3 code to demonstrate working of
# Splitting string into equal halves
# Using list comprehension + string slicing
 
# initializing string
test_str = "GeeksforGeeks"
 
# printing original string
print("The original string is : " + test_str)
 
# Using list comprehension + string slicing
# Splitting string into equal halves
res_first = test_str[0:len(test_str)//2]
res_second = test_str[len(test_str)//2 if len(test_str)%2 == 0
                                else ((len(test_str)//2)+1):]
 
# printing result
print("The first part of string : " + res_first)
print("The second part of string : " + res_second)


Output : 

The original string is : GeeksforGeeks
The first part of string : Geeksf
The second part of string : rGeeks

Time Complexity: O(1) as the string is always split in half at the middle, regardless of its length.

Space Complexity: O(1)

Method #2 : Using string slicing To overcome the shortcomings of above method and find a more elegant solution, we use string slicing to perform this particular task. 

Python3




# Python3 code to demonstrate working of
# Splitting string into equal halves
# Using string slicing
 
# initializing string
test_str = "GeeksforGeeks"
 
# printing original string
print("The original string is : " + test_str)
 
# Using string slicing
# Splitting string into equal halves
res_first, res_second = test_str[:len(test_str)//2], test_str[len(test_str)//2:]
 
# printing result
print("The first part of string : " + res_first)
print("The second part of string : " + res_second)


Output

The original string is : GeeksforGeeks
The first part of string : Geeksf
The second part of string : orGeeks

Method #3: Using islice

The islice() function is used to slice an iterator by specifying the start and stop indices. Here, we use the islice() function to slice the input string twice. The first slice is from the start of the string to the middle and the second slice is from the middle to the end of the string. Then, we use the join() function to join the characters of the sliced substrings and obtain our two final substrings.

Python3




from itertools import islice
 
# Initializing string
test_str = "GeeksforGeeks"
 
# Printing original string
print("The original string is : " + test_str)
 
# Using islice
res_first = ''.join(islice(test_str, None, len(test_str) // 2))
res_second = ''.join(islice(test_str, len(test_str) // 2, None))
 
# Printing result
print("The first part of string : " + res_first)
print("The second part of string : " + res_second)
#This code is contributed by Edula Vinay Kumar Reddy


Output

The original string is : GeeksforGeeks
The first part of string : Geeksf
The second part of string : orGeeks

Time Complexity: O(n) where n is the length of the string.
Auxiliary Space: O(n) as we are creating a new string on each slice.

Method #4: Using the divmod() function

Step-by-step approach:

  • Use the divmod() function to divide the length of the string by 2, which gives us the quotient and remainder. The quotient represents the length of the first part of the string, while the remainder represents the length of the second part of the string. We add the remainder to the quotient to ensure that the first part is longer by one character if the length of the string is odd.
  • Use string slicing to extract the first and second halves of the string. The [:quotient + remainder] notation means that we want to extract all characters from the beginning of the string up to the end of the first part. The [quotient + remainder:] notation means that we want to extract all characters from the beginning of the second part to the end of the string.

Python3




# Initializing string
test_str = "GeeksforGeeks"
 
# Printing original string
print("The original string is : " + test_str)
 
# Using divmod() function
quotient, remainder = divmod(len(test_str), 2)
res_first = test_str[:quotient + remainder]
res_second = test_str[quotient + remainder:]
 
# Printing result
print("The first part of string : " + res_first)
print("The second part of string : " + res_second)


Output

The original string is : GeeksforGeeks
The first part of string : Geeksfo
The second part of string : rGeeks

The time complexity of this method is O(1), as it involves a constant number of operations.
The auxiliary space complexity of this method is O(1), as it only uses a constant amount of additional memory to store the variables.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads