Python Program to Split Each Word According to Given Percent Using Various Methods














































Python Program to Split Each Word According to Given Percent Using Various Methods



PROBLEM STATEMENT:

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:

Input : test_str = 'cppsecrets is best for all geeks and cs students', per_splt = 50

Output :cppse crets 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 ='cppsecrets is best for all geeks and cs students', per_splt = 70

Output : cppse crets 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()

# initializing string
test_str =
'God Is Great'

# 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:




Method 2: Using join() 

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

# Initializing String
test_str =
'God Is Great'

# Printing the 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 the result
print(
"Segmented words : " + str(res))


OUTPUT:


More Articles of RISHABH GURJAR:

Name Views Likes
Stock Price Prediction of Apple Inc Using Recurrent Neural Network 664 6
Solution to the Dining Philosophers Problem using Python 1630 2
Python Program to Remove Elements From an Array Using Array Module 386 2
Solution to N-Queens Problem using Recursion 378 3
Solution to Tower of Hanoi Problem Using Stacks 1222 5
Python Program to Find Most Repeated Word in a Text File 511 3
Python Program to Change MAC Address of Linux Machine 414 3
Python Program to Print Words from a Sentence with Highest and Lowest ASCII Value of Characters 425 3
Python Program to Remove a Specific Digit from Every Element of the List 667 3
Python Program to Split Each Word According to Given Percent Using Various Methods 457 3
Python Program to Compute Logistic Curve Using Theano Module 318 3
Python Program to Add Two Scalar Quantities using Theano Module 265 3
Python Program to Add Two Matrices using Theano Module 307 3
Python Program to Subtract Two Scalar Quantities using Theano Module 326 3
Introduction to Theano Library in Python 361 3
Python Program to Update an Element in Array using Array Module 418 3
Python Program to Search an Element from an Array using Array Module 648 3
Python Program to Slice an Array Using Array Module 417 2
Python Program to Access an Elements from an Array using Array Module 449 3
Python Program to Add Elements to an Array using Array Module 466 3
Creation of Array using Array Module 541 3
Python Program to Display Prime Numbers Between Two Intervals 552 3
Python Program to Find Derivative, Integration, Limits and Quadratic Equation using SymPy Module 615 3
Python Program to Declare Symbolic Variables Explicitly using Symbol Method of SymPy Module 578 3
Python Program to Evaluate Expressions using evalf Method of SymPy Module 752 3
Python Program to Represent Rational Number and Integers using SymPy Module 502 2
Python Program to obtain shell-style pattern converted to a regular expression for using with re.match() 481 2
Python Program to Perform Case-sensitive Comparison of Pattern using fnmatch Module 517 3
Python Program to Filter All the Files Starting with Same Pattern using fnmatch Module 553 3
Python | fnmatch | Searching all files ending with .py 503 3

Comments