Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Python Program to Split each word according to given percent

  • Difficulty Level : Hard
  • Last Updated : 06 Jun, 2021

Given Strings with words, the task is to write a Python program to split each word into two halves on the basis of assigned percentages according to the given values.

Example:

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course

Input : test_str = ‘geeksforgeeks is best for all geeks and cs students’, per_splt = 50



Output : geeksf orgeeks i s be st f or a ll ge eks a nd c s stud ents 

Explanation : Each word after splitting by 50 percent, result is output.

Input : test_str = ‘geeksforgeeks is best for all geeks and cs students’, per_splt = 70

Output : geeksforg eeks i s be st fo r al l gee ks an d c s stude nts 

Explanation : Each word after splitting by 70 percent, result is output.

Method 1: Using split() + len() + slice + join()

In this, we split each word and perform a percentage split of each word using len() and slicing. The result is joined in an intermediate fashion using a loop.

Python3






# Python3 code to demonstrate working of
# Split each word into percent segment in list
# Using split() + len() + slice + loop
  
# initializing string
test_str = 'geeksforgeeks is best for all geeks and cs students'
  
# printing original string
print("The original string is : " + str(test_str))
  
# initializing percent split 
per_splt = 50
  
test_str = test_str.split()
res = ''
for ele in test_str:
    prop = int((per_splt/100) * len(ele))
    new_str1 = ele[:prop]
    new_str2 = ele[prop:]
    res += new_str1 + " " + new_str2 + " "
      
# printing result
print("Segmented words : " + str(res))


Output:

The original string is : geeksforgeeks is best for all geeks and cs students

Segmented words : geeksf orgeeks i s be st f or a ll ge eks a nd c s stud ents 

Method 2: Using join() 

Similar to the above method, the difference is that join() is used to concatenate the resultant string.

Python3




# Python3 code to demonstrate working of
# Split each word into percent segment in list
# Using join()
  
# initializing string
test_str = 'geeksforgeeks is best for all geeks and cs students'
  
# printing original string
print("The original string is : " + str(test_str))
  
# initializing percent split 
per_splt = 50
  
test_str = test_str.split()
  
# one liner solution using join()
res = ' '.join([ele[:int((per_splt/100) * len(ele))] 
                + " " + ele[int((per_splt/100) * len(ele)):] 
                for ele in test_str])
  
# printing result
print("Segmented words : " + str(res))


Output:

The original string is : geeksforgeeks is best for all geeks and cs students

Segmented words : geeksf orgeeks i s be st f or a ll ge eks a nd c s stud ents 




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!