
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Program to rearrange spaces between words in Python
Suppose we have a string s with some words that are placed among some number of spaces. Each words are separated by at least one space. We have to rearrange the spaces so that there are same number of spaces between every pair of adjacent words and the number of spaces between each word is maximized. If we are unable to redistribute all the spaces equally, we can place the extra spaces at the end.
So, if the input is like s = " I love programming ", then the output will be "I love programming ", see the spaces are distributed, between words, there are five spaces.
To solve this, we will follow these steps −
res := blank string
total_sp := number of spaces in s
suff_sp_cnt := total_sp
text_array := a list of words from s
num_words := size of text_array
if num_words is same as 1, then
res := text_array[0] concatenate with total_sp number of spaces
return res
sep_size := quotient of total_sp /(num_words - 1)
sep := sep_size number of spaces
for each i in text_array - 1, do
res := res + i
res := res + sep
suff_sp_cnt := suff_sp_cnt - sep_size
suff_sp_cnt := suff_sp_cnt + sep_size
res := remove extra spaces from left and right
res := res concatenate suff_sp_cnt number of spaces at the end
return res
Example (Python)
Let us see the following implementation to get better understanding −
def solve(s): res = "" total_sp = s.count(" ") suff_sp_cnt = total_sp text_array = s.split() num_words = len(text_array) if num_words == 1: res = text_array[0] + total_sp * " " return res sep_size = total_sp // (num_words - 1) sep = sep_size * " " for i in text_array: res += i res += sep suff_sp_cnt -= sep_size suff_sp_cnt += sep_size res = res.strip() res += suff_sp_cnt * " " return res s = " I love programming " print(solve(s))
Input
" I love programming "
Output
"I love programming "
- Related Articles
- Regex in Python to put spaces between words starting with capital letters
- c# Put spaces between words starting with capital letters
- Rearrange Words in a Sentence in C++
- Lambda expression in Python Program to rearrange positive and negative numbers
- Program to check we can rearrange array to make difference between each pair of elements same in Python
- Python program to count the number of spaces in string
- Python program to count words in a sentence
- Python program to move spaces to front of string in single traversal
- Python program to Count words in a given string?
- Python program to sort Palindrome Words in a Sentence
- Python program to remove K length words in String
- Smallest Distance Between Two Words in Python
- Python program to print even length words in a string
- Count words in a sentence in Python program
- Python Pandas - Rearrange levels in MultiIndex
