# How to split a string into equal half in Python?

When working with text data in Python, you might come across scenarios where you need to split a string into equal halves. In this blog post, we’ll guide you through the process of doing just that, with real examples to make things easy to understand. By the end, you’ll be able to confidently split strings into equal parts using Python, even if the length of the string is odd.

Let us discuss on how to split a string into equal half in Python.

Table of Contents

## Split a string into equal half in Python

There are multiple ways to split a string into equal halves in Python, but we’ll be focusing on a simple, effective method that uses slicing. Slicing in Python allows you to extract a portion of a string based on its index.

Here’s a step-by-step guide to split a string into equal halves:

1. Determine the length of the string using the `len()` function.
2. Calculate the middle index of the string.
3. If the string length is odd, decide whether to round the middle index up or down.
4. Use slicing to extract the two equal halves of the string.

Example-1:

Consider the following string, `example_string`:

``example_string = "splitmeintwohalves"``

Now, let’s walk through the steps outlined above to split this string into equal halves.

Step 1: Determine the length of the string

``````string_length = len(example_string)
print(string_length)  # Output: 18``````

Step 2: Calculate the middle index

``````middle_index = string_length // 2
print(middle_index)  # Output: 9``````

Step 3: Since the length of our example string is even, we don’t need to worry about rounding.

Step 4: Use slicing to extract the two equal halves of the string

``````first_half = example_string[:middle_index]
second_half = example_string[middle_index:]

print(first_half)  # Output: "splitmein"
print(second_half)  # Output: "twohalves"``````

This is how to split a string into equal half in Python.

Another Example:

Here is another example of a string in Python that we split into two halves.

``````text = "United States of America"

# Find the middle index of the string
mid_index = len(text) // 2

# If the string has an odd length, adjust the middle index to split the string as evenly as possible
if len(text) % 2 != 0:
mid_index += 1

# Split the string into equal halves
first_half = text[:mid_index]
second_half = text[mid_index:]

# Print the results
print("First half:", first_half)
print("Second half:", second_half)``````

In this case, the output will be:

``````First half: United Stat
Second half: es of America``````

The string “United States of America” has 23 characters, which is an odd number. Therefore, the first half will have 12 characters and the second half will have 11 characters.

## Example-2: Split a string into equal half in Python (Odd Length)

When working with strings of odd length in Python, you’ll need to decide whether to round the middle index up or down. Here’s an example with an odd-length string in Python:

``````odd_example_string = "splitmeintwohalve"

string_length = len(odd_example_string)
middle_index = string_length // 2

# Round the middle index up
rounded_up_middle_index = middle_index + 1

first_half_rounded_up = odd_example_string[:rounded_up_middle_index]
second_half_rounded_up = odd_example_string[rounded_up_middle_index:]

print(first_half_rounded_up)  # Output: "splitmein"
print(second_half_rounded_up)  # Output: "twohalve"

# Round the middle index down
first_half_rounded_down = odd_example_string[:middle_index]
second_half_rounded_down = odd_example_string[middle_index:]

print(first_half_rounded_down)  # Output: "splitmei"
print(second_half_rounded_down)  # Output: "ntwohalve"``````

In this example, we demonstrated how to handle odd-length strings by rounding the middle index up or down in Python.

## Example-3: Split a string into equal half in Python (Using divmod() function)

Let’s explore another approach to split a string into equal halves using a function with the help of the `divmod()` function in Python. The Python `divmod()` function takes two arguments and returns a tuple containing the quotient and the remainder when the first argument is divided by the second argument.

``````def split_string_into_halves(s: str) -> tuple:
length = len(s)
middle_index, is_odd = divmod(length, 2)

if is_odd:
middle_index += 1

first_half = s[:middle_index]
second_half = s[middle_index:]

return first_half, second_half

example_string = "splitmeintwohalves"
first_half, second_half = split_string_into_halves(example_string)

print(first_half)  # Output: "splitmein"
print(second_half)  # Output: "twohalves"``````

In this example, we’ve created a function called `split_string_into_halves()` that accepts a string `s` as an argument and returns a tuple containing the two halves of the string. The function first calculates the middle index and the remainder by calling `divmod()`. If the remainder is non-zero (i.e., the string length is odd), we increment the middle index by 1 to round up. Finally, the function slices the string into two equal halves using the middle index in Python.

Here’s how the function can be used with an odd-length string:

``````odd_example_string = "splitmeintwohalve"
first_half, second_half = split_string_into_halves(odd_example_string)

print(first_half)  # Output: "splitmein"
print(second_half)  # Output: "twohalve"``````

By using the `split_string_into_halves()` function, you can easily split strings into equal halves in Python in a concise and reusable manner.

In this tutorial, we have learned how to split a string into equal half in Python. We saw here different ways to splitting a Python string into equal halves.

You may also like the following Python tutorials: