Bird
0
0

You want to create a custom module text_utils.py with a function count_words(text) that returns the number of words in a string. How would you write and use this module in another file?

hard📝 Application Q8 of 15
Python - Modules and Code Organization
You want to create a custom module text_utils.py with a function count_words(text) that returns the number of words in a string. How would you write and use this module in another file?
AWrite the function in text_utils.py and use <code>include text_utils</code>
BWrite the function in text_utils.py and call it directly without import
CWrite the function in text_utils.py and import using <code>import text_utils.count_words</code>
DWrite the function in text_utils.py and import it using <code>from text_utils import count_words</code>
Step-by-Step Solution
Solution:
  1. Step 1: Write function in text_utils.py

    Define count_words(text) that returns word count.
  2. Step 2: Import function correctly

    Use 'from text_utils import count_words' to use function directly.
  3. Final Answer:

    Write the function in text_utils.py and import it using from text_utils import count_words -> Option D
  4. Quick Check:

    Correct import syntax = from module import function [OK]
Quick Trick: Use 'from module import func' to access functions easily [OK]
Common Mistakes:
  • Trying to call functions without import
  • Using invalid import syntax like import module.func
  • Using include which is not Python syntax

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes