Python Program to Remove Spaces from String

Python Program to Remove Spaces from String

  • 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.

Source Code:

# Python Program - Remove Spaces from String

print("Enter 'x' for exit.")
string = input("Enter any string to remove all spaces: ")
if string == 'x':
    exit()
else:
    new_string = string.replace(" ","")
    print("\nNew string after removing all spaces:")
    print(new_string)   


 

Comments