input_string = input("Enter a string: ")
output_string = input_string.replace(" ", "-")
print("Modified string:", output_string)
#source code --> clcoding.com
Code Explanation:
input_string = input("Enter a string: ")
input():
Prompts the user to enter a string.
The prompt text "Enter a string: " is displayed to the user in the console.
The user's input is returned as a string.
input_string:
The string entered by the user is stored in the variable input_string.
output_string = input_string.replace(" ", "-")
input_string.replace(" ", "-"):
The replace() method is called on the string stored in input_string.
It replaces every occurrence of a blank space (" ") in the string with a hyphen ("-").
The method returns a new string with the replacements. It does not modify the original string because strings in Python are immutable.
output_string:
The modified string (where all spaces are replaced with hyphens) is stored in the variable output_string.
print("Modified string:", output_string)
print():
Outputs the modified string to the console.
The string "Modified string:" serves as a label.
output_string:
The value of output_string (the modified string) is displayed next to the label.
0 Comments:
Post a Comment