Value-Returning Function V2 - Python Programming Exercise

In this exercise, you will 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). This exercise is perfect for practicing function definition and calling in Python. By implementing this function and calling it from the main function, you will gain hands-on experience in handling function definitions and calls in Python. This exercise not only reinforces your understanding of functions but also helps you develop efficient coding practices for managing user interactions.

 Category

Mastering Functions

 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

 Copy 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

 More Python Programming Exercises of Mastering Functions

Explore our set of Python Programming Exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of Python. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in Python.

  •  Centered Text Function

    In this exercise, you will develop a Python function named "get_int" that displays the text received as a parameter, prompts the user for an integer number, repeats i...

  •  Underline Text Function

    In this exercise, you will develop a Python function that can center the text on the screen (assuming a screen width of 80 characters) and then underline it with hyph...

  •  Array Summation Function

    In this exercise, you will develop a Python program to calculate the sum of the elements in an array. The main function should look like this: def main(): example = [...

  •  Double Value Function

    In this exercise, you will develop a Python function named "double" to calculate and return an integer that is twice its input. This exercise is perfect for pr...

  •  Function Double with Mutable Parameter

    In this exercise, you will develop a Python function named "double_value" to calculate the double of an integer number and modify the data passed as an argument. Sinc...

  •  Function Swap with Mutable Parameters

    In this exercise, you will develop a Python program with a function called "swap" to exchange the values of two integer variables, which are passed by reference. This...