Exercise
Value-Returning Function V2
Objective
Develop a Python program where the main function should look like this:
def main():
print("\"Hello, how are you\" contains {0} spaces".format(count_spaces("Hello, how are you")))
You must define the function `count_spaces`, and it will be called from within the main function.
As shown in the example, it must accept a string as a parameter and return an integer (the number of spaces in that string).
Example Python Exercise
Show Python Code
# Define the count_spaces function that accepts a string as a parameter
def count_spaces(input_string):
# Count the number of spaces in the input string using the count method
return input_string.count(' ') # This will return the count of spaces in the string
# Main function to execute the program
def main():
# Print the result using the format method, calling count_spaces with a string as argument
print("\"Hello, how are you\" contains {0} spaces".format(count_spaces("Hello, how are you"))) # It will print the number of spaces in the string
# Call the main function to run the program
main() # This triggers the execution of the count_spaces function and prints the result
Output
"Hello, how are you" contains 4 spaces
Share this Python Exercise