Python | Split given string into equal halves Last Updated : 13 Jan, 2025 Comments Improve Suggest changes Like Article Like Report We are given a string, we need to split it into two halves. If the string has an odd length, the first half should be longer by one character.Using String Slicing String Slicing is the efficient approach which splits the string at the midpoint. If the string length is odd, the first half automatically gets the extra character. Python s1 = "GeeksforGeeks" # Use string slicing to split the string into first and second half s2, s3 = s1[:len(s1)//2 + len(s1)%2], s1[len(s1)//2 + len(s1)%2:] print("First half:", s2) print("Second half:", s3) OutputFirst half: Geeksfo Second half: rGeeks Explanation:s1[:len(s1)//2 + len(s1)%2]: Extracts the first half, adding 1 character if the string has an odd length.s1[len(s1)//2 + len(s1)%2:]: Extracts the second half, starting from the midpoint.Using the divmod() function divmod() function divides the string length by 2, obtaining the quotient (length of the first part) and remainder. Add the remainder to the quotient if the length is odd. Use slicing to extract the first half as [:q + r] and the second half as [q + r:] Python s = "GeeksforGeeks" # Using divmod to get the quotient (q) and remainder (r) when dividing the string length by 2 q, r = divmod(len(s), 2) # Slicing the string to get the first half, including the remainder if the length is odd first, second = s[:q + r], s[q + r:] print("First half:", first) print("Second half:", second) OutputFirst half: Geeksfo Second half: rGeeks Explanation:divmod(len(s), 2) divides the length of s by 2, returning the quotient (q) as the midpoint and the remainder (r) to ensure the first half is longer if the length is odd.first = s[:q + r] extracts the first half of the string, adding an extra character if the length is odd.second = s[q + r:] extracts the second half, starting from the midpoint. Using islice() from itertoolsThis method uses islice() to split the string into two halves. It slices the string twice: the first slice from the start to the middle, and the second slice from the middle to the end. The join() function is used to convert the iterator into a string. Python from itertools import islice s1 = "GeeksforGeeks" # Use islice to get the first half of the string s2 = ''.join(islice(s, None, len(s)//2 + len(s)%2)) # Use islice to get the second half of the string s3 = ''.join(islice(s, len(s)//2 + len(s)%2, None)) print("First half:", s2) print("Second half:", s3) OutputFirst half: Geeksfo Second half: rGeeks Explanation:s2 = ''.join(islice(s, None, len(s)//2 + len(s)%2)) extracts the first half of the string. The slice starts from the beginning (None) to the middle, adding an extra character if the string length is odd (due to len(s)%2).s3 = ''.join(islice(s, len(s)//2 + len(s)%2, None)) extracts the second half, starting from the midpoint to the end of the string. Comment More infoAdvertise with us Next Article Python | Split given string into equal halves manjeet_04 Follow Improve Article Tags : Python Python Programs Python string-programs Practice Tags : python Similar Reads Python | Exceptional Split in String Sometimes, while working with Strings, we may need to perform the split operation. The straightforward split is easy. But sometimes, we may have a problem in which we need to perform split on certain characters but have exceptions. This discusses split on comma, with the exception that comma should 4 min read Split and join a string in Python The goal here is to split a string into smaller parts based on a delimiter and then join those parts back together with a different delimiter. For example, given the string "Hello, how are you?", you might want to split it by spaces to get a list of individual words and then join them back together 3 min read Python | Split given dictionary in half While working with dictionaries, sometimes we might have a problem in which we need to reduce the space taken by single container and wish to divide the dictionary into 2 halves. Let's discuss certain ways in which this task can be performed. Method #1 : Using items() + len() + list slicing The comb 6 min read Split a String by a Delimiter in Python In Python Programming, the split() method is used to split a string into a list of substrings which is based on the specified delimiter. This method takes the delimiter as an argument and returns the list of substrings. Along with this method, we can use various approaches wot split a string by the 2 min read Python | Split a list into sublists of given lengths The problem of splitting a list into sublists is quite generic but to split in sublist of given length is not so common. Given a list of lists and list of length, the task is to split the list into sublists of given length. Example: Input : Input = [1, 2, 3, 4, 5, 6, 7] length_to_split = [2, 1, 3, 1 2 min read Python - Split a String by Custom Lengths Given a String, perform split of strings on the basis of custom lengths. Input : test_str = 'geeksforgeeks', cus_lens = [4, 3, 2, 3, 1] Output : ['geek', 'sfo', 'rg', 'eek', 's'] Explanation : Strings separated by custom lengths.Input : test_str = 'geeksforgeeks', cus_lens = [10, 3] Output : ['geeks 2 min read Python | Splitting operators in String Sometimes we have a source string to have certain mathematical statement for computations and we need to split both the numbers and operators as a list of individual elements. Let's discuss certain ways in which this problem can be performed. Method #1 : Using re.split() This task can be solved usin 7 min read Split String of list on K character in Python In this article, we will explore various methods to split string of list on K character in Python. The simplest way to do is by using a loop and split().Using Loop and split()In this method, we'll iterate through each word in the list using for loop and split it based on given K character using spli 2 min read Divide String into Equal K chunks - Python The task is to split the string into smaller parts, or "chunks," such that each chunk has exactly k characters. If the string cannot be perfectly divided, the last chunk will contain the remaining characters.Using List ComprehensionList comprehension allows for creating a new list by applying an exp 2 min read Split String into List of characters in Python We are given a string and our task is to split this string into a list of its individual characters, this can happen when we want to analyze or manipulate each character separately. For example, if we have a string like this: 'gfg' then the output will be ['g', 'f', 'g'].Using ListThe simplest way t 2 min read Like