Python Program to Split each Word According to given Percent


Python is a popular programming language known for its simplicity and versatility. It is widely used in various industries, including web development, data science, and machine learning. One of the many advantages of Python is its ability to easily manipulate strings and text data. In this tutorial, we will explore how to split each word according to a given percentage in Python.

Explanation of the Problem

The problem we're trying to solve in this tutorial is splitting each word in a string into parts based on a given percentage value. This can be useful in many applications, such as data analysis or text processing.

For instance, let's say we have the following sentence: "I love Python programming language". We want to split each word in the sentence into two parts with a 50% split. This means each word will be split into two equal parts.

Input

I love Python programming language

Output

I lo ve Pyt hon progr amming lang uage

In the next section of the article, we'll explore multiple methods for implementing this solution in Python.

Approach 1: Using fixed length slicing function

We will explore the first approach to split each word according to a given percentage in Python. This method involves using fixed−length slicing to divide the words based on the specified percentage.

The logic behind this approach is quite straightforward. We will iterate through each word in the sentence and calculate the desired length for splitting based on the given percentage. Then, using slicing, we will divide the word into two parts: one representing the specified percentage and the other representing the remaining portion.

Let's dive into the implementation with the following code:

def split_words_fixed_length(sentence, percent):
    words = sentence.split()
    result = []
    
    for word in words:
        length = int(len(word) * (percent / 100))
        part1 = word[:length]
        part2 = word[length:]
        result.append(part1 + " " + part2)
    
    return result

# Example usage
sentence = "I love Python Programming language."
percent = 50
split_result = split_words_fixed_length(sentence, percent)

# Print the results
print(" ".join(split_result))

Output

I lo ve Py thon Pro gramming lan guage.

In the code above, we first split the sentence into individual words using the `split()` function. Then, for each word, we calculate the desired length for splitting by multiplying the length of the word by the given percentage divided by 100. Finally, we join the elements of the `split_result` list using a space and print the output. This results in all the words split according to the given percentage appearing on a single line.

Approach 2: Using Regular Expression Splitting

In this section, we will explore another approach to split each word according to a given percent using regular expressions in Python. Regular expressions (regex) provide a powerful and flexible way to match patterns in text, making them suitable for splitting words based on specific criteria.

The logic behind this approach involves utilizing a regular expression pattern to match the desired splitting point in each word. We will construct a pattern that represents the position where we want to split the word based on the given percentage.

Let's dive into the implementation with the following code:

import re

def split_words_regex(sentence, percent):
    res = ' '.join([re.sub(r'(\w{' + str(int((percent/100) * len(ele))) + r'})(\w+)',
                    r'\1 \2', ele)
                for ele in sentence.split()])
    return res

# Example usage
sentence = "I love Python Programming language."
percent = 50
split_result = split_words_regex(sentence, percent)

# Print the result
print(split_result)

Output

I lo ve Py thon Progr amming lang uage.

In the code above, we import the `re` module, which provides functionality for working with regular expressions. We define the `split_words_regex` function, which takes the `sentence` and `percent` as input parameters.

This regex pattern matches a word followed by a specified number of word characters (`\w`) based on the calculated length from the first word and the given percentage.

We use `re.sub()` to substitute the matched pattern with a space. The `split_result` variable stores the modified sentence with the split words.

Finally, we print the `split_result`, which contains the sentence with each word split according to the given percentage.

Conclusion

In this tutorial, we explored different approaches to splitting each word according to a given percentage in Python. By using fixed−length slicing and regular expressions, we demonstrated how to achieve the desired result. These techniques provide flexibility and efficiency when working with text data. Whether you need to divide words for data analysis, text processing, or any other application, the methods presented in this tutorial can be easily adapted to suit your specific requirements.

Updated on: 25-Jul-2023

98 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements