192,510 views
10 votes
10 votes
Write a program that asks the user for a sentence and creates a new string based on the input that has all the spaces removed. For example if the user enters: “This is a test” It should output: “Thisisatest”

User Aashif Ahamed
by
3.0k points

1 Answer

11 votes
11 votes

Answer:

Here is a Python program that asks the user for a sentence and creates a new string based on the input that has all the spaces removed.

# Prompt the user for a sentence

sentence = input("Enter a sentence: ")

# Create a new string with all the spaces removed

no_spaces = sentence.replace(" ", "")

# Print the new string

print(f"New string: {no_spaces}")

Step-by-step explanation:

In this program, we first prompt the user for a sentence and store it in the sentence variable. We then use the replace method to create a new string with all the spaces removed. This method takes two arguments: the string to search for (in this case, a space), and the string to replace it with (in this case, an empty string).

Finally, we print the new string that has all the spaces removed. This program allows the user to input any sentence, and creates a new string based on that input that has all the spaces removed.

User Masjum
by
3.0k points