
- 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
Python code to move spaces to the front of string in a single traversal
We have a string, and our goal is to move all the spaces in the string to front. Suppose if a string contains four spaces then, we have to move those four spaces in front of every character. Let's see some sample test cases before going to the coding.
Input: string = "tutorials point " Output: "tutorialspoint" -> output will be without quotes
Input: string = "I am a python programmer." Output: "Iamapythonprogrammer." -> output will be without quotes
Let's follow the below steps to achieve our goal.
Algorithm
1. Initialise the string. 2. Find out all the characters which are not spaces and store them in a variable. 3. Find out the no. of spaces by count method of the string. 4. Multiply a space by no. of spaces and store it in a variable. 5. Append all the characters to the previous variable. 6. Print the result at the end.
Let's try to implement the above algorithm.
Example
## initializing the string string = "tutorials point " ## finding all character exclusing spaces chars = [char for char in string if char != " "] ## getting number of spaces using count method spaces_count = string.count(' ') ## multiplying the space with spaces_count to get all the spaces at front of the ne w_string new_string = " " * spaces_count ## appending characters to the new_string new_string += "".join(chars) ## priting the new_string print(new_string)
Output
If you run the above program, you will get the following output.
tutorialspoint
Let's execute the program with different input.
Example
## initializing the string string = "I am a python programmer." ## finding all character exclusing spaces chars = [char for char in string if char != " "] ## getting number of spaces using count method spaces_count = string.count(' ') ## multiplying the space with spaces_count to get all the spaces at front of the ne w_string new_string = " " * spaces_count ## appending characters to the new_string new_string += "".join(chars) ## priting the new_string print(new_string)
Output
If you run the above program, you will get the following output.
Iamapythonprogrammer.
Conclusion
If you have any doubts regarding the program, mention them in the comment section.
- Related Articles
- Python program to move spaces to front of string in single traversal
- Move string capital letters to front maintaining the relative order - JavaScript
- Python program to count the number of spaces in string
- How to replace multiple spaces in a string using a single space using Java regex?
- Python Program to Replace the Spaces of a String with a Specific Character
- Python Program to move numbers to the end of the string
- Move all zeros to the front of the linked list in C++
- Move last element to front of a given Linked List in C++
- How to expand tabs in string to multiple spaces in Python?
- Execute a String of Code in Python
- How to replace multiple spaces with a single space in C#?
- Count minimum number of “move-to-front” moves to sort an array in C++
- How to join two strings to convert to a single string in Python?
- How to strip all spaces out of a string in PHP?
- How to remove all special characters, punctuation and spaces from a string in Python?

Advertisements