HomePythonPython Program to Replace Every Blank Space with Hyphen in a String

Python Program to Replace Every Blank Space with Hyphen in a String

This Python program replaces every blank space in a given string with a hyphen ‘-‘.

Program Statement

Write a Python program that replaces every blank space with a hyphen (“-“) in a given string.

The program should:

  1. Accept a string as input from the user.
  2. Replace every occurrence of a blank space (” “) with a hyphen (“-“) in the input string.
  3. Print the modified string as the output.

The program should be able to handle strings of any length and can be used for various applications where replacing blank spaces with hyphens is required.

You can implement this program using the replace() method in Python, as demonstrated in the earlier code example.

Python Program to Replace Every Blank Space with Hyphen in a String

def replace_spaces_with_hyphen(string):
    replaced_string = string.replace(" ", "-")
    return replaced_string

# Example usage
input_string = "Hello World! This is a test."
output_string = replace_spaces_with_hyphen(input_string)
print(output_string)

How it works

Let’s go through how the Python program works:

  1. The program starts by defining a function called replace_spaces_with_hyphen that takes a string as input.
  2. Inside the function, the replace() method is used on the input string. The replace() method takes two arguments: the substring to be replaced and the substring to replace it with. In this case, we want to replace every occurrence of a blank space (” “) with a hyphen (“-“), so we pass these two values as arguments to the replace() method.
  3. The modified string is then assigned to the variable replaced_string.
  4. Finally, the replaced_string is returned as the output of the function.
  5. After defining the function, the program prompts the user to enter a string by using the input() function. The entered string is stored in the variable input_string.
  6. The program then calls the replace_spaces_with_hyphen() function, passing the input_string as an argument. The function is executed, and the modified string is returned.
  7. The modified string is assigned to the variable output_string.
  8. Lastly, the program prints the output_string using the print() function, displaying the string with hyphens replacing the blank spaces.

By following these steps, the program takes a string as input, replaces the blank spaces with hyphens using the replace() method, and then prints the modified string as the output.

Input/Output

Python Program to Replace Every Blank Space with Hyphen in a String

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

In this Python program, we will create a singly linked list and remove duplicate elements from it. A linked list...
This Python program solves the Celebrity Problem by finding a person who is known by everyone but does not know...
This Python program uses a recursive approach to solve the n-Queens problem. It explores all possible combinations of queen placements...