def replace_spaces(s):
return s.replace(" ", "_")
print(replace_spaces("Hello World"))
print(replace_spaces("Python is fun"))
print(replace_spaces("NoSpacesHere"))
#source code --> clcoding.com
Code Explanation:
Define the function
def replace_spaces(s): → Creates a function replace_spaces that takes a string s as input.
Replace spaces using .replace()
s.replace(" ", "_") → Finds all spaces " " in the string and replaces them with underscores "_".
Return the modified string
The function returns the updated string with all spaces replaced.
Call the function and print results
replace_spaces("Hello World") → Returns "Hello_World".
replace_spaces("Python is fun") → Returns "Python_is_fun".
replace_spaces("NoSpacesHere") → Returns "NoSpacesHere" (unchanged, as there were no spaces).
0 Comments:
Post a Comment