Python Split a String in Half

While programming in Python, you may want to divide the string into two evenly spaced parts occasionally while handling the data inputs and are stuck in doing them. Splitting the input strings involves reducing the string from the middle. Suppose the specified string’s first half includes some characters when the count or string becomes odd. To access the elements, utilize the string indexes.

In this write-up, we will talk about different methods for splitting a Python string in half.

How to Split a String From Half in Python?

Multiple methods are used to split the Python string from half, which is as follows:

Method 1: Split a String From Half in Python by Utilizing “for” Loop

Utilize the “for” loop for splitting a Python string from the half. The “for” iterative function that used to iterate over any specified sequence. Likewise, string, tuple, or list.

Example

Firstly, define the function as “my_string” with one argument:

def my_string(value):

 

Then, create two empty string variables as “firstString” and “secondString”:

firstString = ''
secondString = ''

 

Execute the “for” loop with “if” condition that will check if the index value is less than half of the original string length and returns the string into two equal splitted parts:

for i in range(0,len(value)):
    if i <= len(value)//2-1:
    firstString = firstString + value[i]
    else:
    secondString = secondString + value[i]
return firstString, secondString

 

After that, string variable is created and pass a desired string:

original_string = 'LinuxWorld'

 

Print the original string by calling the “print()” statement:

print('My string is:', original_string)

 

Lastly, get the previously specified string into equal half parts:

print('Split the string from half:', my_string(original_string))

 

Output

Method 2: Split a String From Half in Python by Utilizing String Slicing Method

The string slicing technique can be used for splitting any string in Python. It can divide Python strings into equal halves. To do so, check the provided example.

Example

First, we have accessed the function length by passing the “original_string” string variable to the “len()” method that will return the set of entries in the input string. The length method is called within the “stringLen” variable:

stringLen = len(original_string)

 

Use the “print()” method to get the string length:

print("My String Length:", stringLen)

 

Now, execute the “if” condition to check whether the character present in the given output string is even or not. If the string contains an odd character, we will count for the extra character, resulting in two substrings of not equal string length. Then, use the printed expression to get the split string into two equal halves:

if stringLen%2 == 0:
    firstString = original_string[0:stringLen//2]
    secondString = original_string[stringLen//2:]
    print("String First Half :", firstString)
    print("String Second Half:", secondString)

else:
    firstString = original_string[0:(stringLen//2+1)]
    secondString = original_string[(stringLen//2+1):]
    print("First Half :", firstString)
    print("Second Half :", secondString)

 

As you can see, the provided string has been splitted into two half successfully:

Method 3: Split a String From Half in Python by Utilizing “slice()” Method

Another way to split a particular Python string from half is to use “slice()”. This method takes two parameters for splitting. The first one specifies the starting point, and the second argument denotes the ending point of any string.

Example

Inside the “if” statement with the length of the string, we have created two string variables that store the slicing values by utilizing the “slice()” method. These variables include the slicing rule along with the desired position of the slicing. After that, we store it as the substring for the input string needed to slice. It will return the value of the required substring:

if stringLen%2 == 0:
  firstString = slice(0, stringLen//2)
  secondString = slice(stringLen//2, stringLen)
  print("First Half of String:", original_string[firstString])
  print("Second Half of String:", original_string[secondString])
else:
  firstString = slice(0, stringLen//2)
  secondString = slice(stringLen//2, stringLen)
  print("First Half of String:", original_string[firstString])
  print("Second Half of String:", original_string[secondString])

 

According to the following output, the provided string is splitted in half:

Method 4: Split a String From Half in Python by Utilizing “split()” Method

Use the “split()” method to split any Python string from half. It can divide the desired string based on the character within the particular string and returns the substrings list after dividing it.

Example

First, the string variable is created and initialized with the string value:

original_string = 'LinuxZWorld'

 

Call the “split()” function with the “Z” string character as an argument. The provided string will be split from the specified character and removed from the string. The resultant string will be passed to the “resultant_string” variable:

resultant_string = original_string.split('Z')

 

Then, pass the “resultant_string” variable inside the “print()” method to get the split string into parts:

print(resultant_string)

 

Output

That’s all! We have demonstrated several ways to split a Python string in half.

Conclusion

To split the Python string into half, the iterative function “for” loop, the “slice()” method, the “split()” method, and the “String Slicing” technique are used. When the string splitted in half by utilizing these methods, the string length will be checked and then splitted. This write-up illustrated different techniques to split a Python string in half.



from https://ift.tt/lU4orI5

Post a Comment

0 Comments