codescracker


python

Python Program to Remove Spaces from String



« Previous Program Next Program »


Remove Spaces from String in Python

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.

Python Programming Code to Remove Spaces from String

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

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);

Here is the sample run of the above python program to illustrate how to remove spaces from a string:

remove spaces from string python

Now enter any string say this is what i am to remove all its spaces after pressing enter key as shown here:

remove spaces python

Same program on python shell:

python remove spaces from string

Program Video with Compile and Run

Same Program in Other Languages

You may also like to learn or practice the same program in other popular programming languages:


« Previous Program Next Program »








Tools
Calculator

Quick Links
Signup - Login - Give Online Test