Python | Split given string into equal halves Last Updated : 13 Jan, 2025 Summarize Comments Improve Suggest changes Share 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 M manjeet_04 Follow Improve Article Tags : Python Python Programs Python string-programs Practice Tags : python Similar Reads Python Tutorial - Learn Python Programming Language Python is one of the most popular programming languages. Itâs simple to use, packed with features and supported by a wide range of libraries and frameworks. Its clean syntax makes it beginner-friendly. It'sA high-level language, used in web development, data science, automation, AI and more.Known fo 10 min read Python Interview Questions and Answers Python is the most used language in top companies such as Intel, IBM, NASA, Pixar, Netflix, Facebook, JP Morgan Chase, Spotify and many more because of its simplicity and powerful libraries. To crack their Online Assessment and Interview Rounds as a Python developer, we need to master important Pyth 15+ min read Python OOPs Concepts Object Oriented Programming is a fundamental concept in Python, empowering developers to build modular, maintainable, and scalable applications. By understanding the core OOP principles (classes, objects, inheritance, encapsulation, polymorphism, and abstraction), programmers can leverage the full p 11 min read Python Projects - Beginner to Advanced Python is one of the most popular programming languages due to its simplicity, versatility, and supportive community. Whether youâre a beginner eager to learn the basics or an experienced programmer looking to challenge your skills, there are countless Python projects to help you grow.Hereâs a list 10 min read Python Exercise with Practice Questions and Solutions Python Exercise for Beginner: Practice makes perfect in everything, and this is especially true when learning Python. If you're a beginner, regularly practicing Python exercises will build your confidence and sharpen your skills. To help you improve, try these Python exercises with solutions to test 9 min read Python Programs Practice with Python program examples is always a good choice to scale up your logical understanding and programming skills and this article will provide you with the best sets of Python code examples.The below Python section contains a wide collection of Python programming examples. These Python co 11 min read Python Introduction Python was created by Guido van Rossum in 1991 and further developed by the Python Software Foundation. It was designed with focus on code readability and its syntax allows us to express concepts in fewer lines of code.Key Features of PythonPythonâs simple and readable syntax makes it beginner-frien 3 min read Python Data Types Python Data types are the classification or categorization of data items. It represents the kind of value that tells what operations can be performed on a particular data. Since everything is an object in Python programming, Python data types are classes and variables are instances (objects) of thes 9 min read Input and Output in Python Understanding input and output operations is fundamental to Python programming. With the print() function, we can display output in various formats, while the input() function enables interaction with users by gathering input during program execution. Taking input in PythonPython input() function is 8 min read Enumerate() in Python enumerate() function adds a counter to each item in a list or other iterable. It turns the iterable into something we can loop through, where each item comes with its number (starting from 0 by default). We can also turn it into a list of (number, item) pairs using list().Let's look at a simple exam 3 min read Like