Python code to move spaces to front of string in single traversal

Given a string that has set of words and spaces, write a program to move all spaces to front of string, by traversing the string only once.

Examples:

Input  : str = "geeks for geeks"
Output : ste = "  geeksforgeeks"

Input  : str = "move these spaces to beginning"
Output : str = "    movethesespacestobeginning"
There were four space characters in input,
all of them should be shifted in front. 

This problem has existing solution, please refer Move spaces to front of string in single traversal link.
We will solve this problem quickly in Python using List Comprehension.
Approach:

  1. Traverse input string and create a string without any space character using list comprehension.
  2. Now to know how many space characters were there in original string just take a difference of length of original string and new string.
  3. Now create another string and append space characters at the beginning.
filter_none

edit
close

play_arrow

link
brightness_4
code

# Function to move spaces to front of string
# in single traversal in Python
  
def moveSpaces(input):
      
    # Traverse string to create string without spaces
    noSpaces = [ch for ch in input if ch!=' ']
  
    # calculate number of spaces
    space= len(input) - len(noSpaces)
  
    # create result string with spaces
    result = ' '*space
  
    # concatenate spaces with string having no spaces
    result = '"'+result + ''.join(noSpaces)+'"'
    print result
  
# Driver program
if __name__ == "__main__":
    input = 'geeks for geeks'
    moveSpaces(input)

chevron_right


Output:

"  geeksforgeeks"


My Personal Notes arrow_drop_up

Experienced Software Engineer with a demonstrated history of working in the information technology and services industry Skilled in Big Data technologies, Data Structures, Algorithm, Software Development, App Development, Databases, Python, Java, C, and WordPress

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.