Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python Program to Split each Word According to given Percent
Python provides powerful string manipulation capabilities that allow us to split words according to specific criteria. In this tutorial, we will explore how to split each word in a string based on a given percentage using different approaches.
Problem Explanation
We need to split each word in a sentence into two parts based on a percentage value. For example, with a 50% split, each word gets divided roughly in half ?
Input: "I love Python programming language" with 50% split
Expected Output: "I lo ve Pyt hon progr amming lang uage"
Using Fixed Length Slicing
This approach calculates the split position by multiplying word length with the percentage, then uses string slicing to divide each word ?
def split_words_fixed_length(sentence, percent):
words = sentence.split()
result = []
for word in words:
# Calculate split position based on percentage
split_pos = int(len(word) * (percent / 100))
part1 = word[:split_pos]
part2 = word[split_pos:]
# Add space between parts if both parts exist
if part1 and part2:
result.append(part1 + " " + part2)
else:
result.append(word)
return " ".join(result)
# Example usage
sentence = "I love Python programming language"
percent = 50
split_result = split_words_fixed_length(sentence, percent)
print(split_result)
I lo ve Pyt hon progr amming lang uage
Using Regular Expressions
Regular expressions provide a pattern-based approach to split words. We construct a regex pattern that matches the calculated split position ?
import re
def split_words_regex(sentence, percent):
words = sentence.split()
result = []
for word in words:
# Calculate split position
split_pos = int(len(word) * (percent / 100))
if split_pos > 0 and split_pos < len(word):
# Create regex pattern to match first n characters followed by remaining
pattern = r'(\w{' + str(split_pos) + r'})(\w+)'
modified_word = re.sub(pattern, r'\1 \2', word)
result.append(modified_word)
else:
result.append(word)
return " ".join(result)
# Example usage
sentence = "I love Python programming language"
percent = 60
split_result = split_words_regex(sentence, percent)
print(split_result)
I lo ve Pyth on progra mming langu age
Using List Comprehension
A more concise approach using list comprehension for cleaner, more Pythonic code ?
def split_words_comprehension(sentence, percent):
def split_word(word):
split_pos = int(len(word) * (percent / 100))
if split_pos > 0 and split_pos < len(word):
return word[:split_pos] + " " + word[split_pos:]
return word
return " ".join([split_word(word) for word in sentence.split()])
# Example usage
sentence = "Python programming is amazing"
percent = 40
result = split_words_comprehension(sentence, percent)
print(result)
Py thon prog ramming is amaz ing
Comparison
| Method | Readability | Performance | Best For |
|---|---|---|---|
| Fixed Length Slicing | High | Fast | Simple, straightforward splitting |
| Regular Expressions | Medium | Slower | Complex pattern matching |
| List Comprehension | High | Fast | Concise, Pythonic code |
Conclusion
Fixed length slicing offers the best balance of simplicity and performance for splitting words by percentage. Use regular expressions when you need more complex pattern matching, and list comprehension for clean, readable code.
