Python Program To Replace Blank Space With Hyphen
Chapter:
Python
Last Updated:
30-05-2023 13:27:30 UTC
Program:
/* ............... START ............... */
def replace_spaces_with_hyphens(string):
return string.replace(" ", "-")
# Example usage
input_string = "This is a sample string with blank spaces"
output_string = replace_spaces_with_hyphens(input_string)
print(output_string)
/* ............... END ............... */
Output
This-is-a-sample-string-with-blank-spaces
Notes:
-
We define a function called replace_spaces_with_hyphens that takes a string as input.
- Inside the function, we use the replace method of the string to replace all occurrences of a blank space (" ") with a hyphen ("-").
- The replace_spaces_with_hyphens function is called, passing the input_string as an argument. The returned result is assigned to the output_string variable.
- Finally, the output_string is printed to the console, which will display the modified string with hyphens instead of blank spaces.
- In summary, the program defines a function that replaces blank spaces with hyphens in a given string. It then demonstrates the usage of this function by replacing blank spaces in a sample string and printing the modified string.
Tags
Python program to replace blank space with hyphen #Python program to replace the string space with a given character