To remove all spaces from string or sentence in python, you have to ask from user to enter a string and start removing all the spaces from that string and finally display the string without any spaces as shown in the program given here.
Following python program ask form user to enter any string to remove all spaces from it and print the same string but without any spaces:
# Python Program - Remove Spaces from String while True: print("Enter 'x' for exit.") string = input("Enter any string: ") if string == 'x': break else: new_string = string.replace(" ","") print("\nNew string after removing all spaces:") print(new_string,"\n")
Here is the sample run of the above python program to illustrate how to remove spaces from a string:
You may also like to learn or practice the same program in other popular programming languages: