Python: move spaces to the front of a given string

move spaces to the front of a given string

write a python program to move spaces to the front of a given string.

Program:-

def move_Spaces_front(str1):
noSpaces_char = [ch for ch str1 if ch!= ' ']
spaces_char = len(str1) - len(nospaces_char)
result = ' '*spaces_char
result = '"'+result + ''.join(nospaces_char)+'"'
return(result)

print(move_spaces_front("sirfpadhai . in "))
print(move_spaces_front("   sirfpadhai.in "))

Output:-

"     sirfpadhai.in"
"     sirfpadhai.in"

flowchart:-

Sirf Padhai
Previous articlePython: Split a string on the last occurrence of the delimiter| split string last occurrence python
Next articlePython: Move all spaces to the front of a given string in single traversal

LEAVE A REPLY

Please enter your comment!
Please enter your name here