Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6088365
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:58:34+00:00 2026-05-23T11:58:34+00:00

I am working my way through some samples, and came up with this working

  • 0

I am working my way through some samples, and came up with this working answer – the challenge being set in the doctests:

def remove(sub, s):
    """
      >>> remove('an', 'banana')
      'bana'
      >>> remove('cyc', 'bicycle')
      'bile'
      >>> remove('iss', 'Mississippi')
      'Missippi'
      >>> remove('egg', 'bicycle')
      'bicycle'
    """
    if sub not in s: return s # 1
    from string import replace # 2
    return replace( s, sub, '', 1) # 3

As a PyNoob I’d like to ask a few questions to get my bearings on all of this stuff vs the baggage I brought along with me from PHP.

On my line:

  1. Is that the right place to bring in a module?
    Is there an accepted way of checking the module was loaded and handling it?

2.
Is that the most efficient way of loading just the method I want?

3.
Overall really, is that the way you would solve this particular problem?

I guess I am also hunting for a steer vs your ‘real world’ expyrience. Is the string module so common that you’d more than likely have it loaded anyways?

If that is the case, how do you check a module is loaded?

Would it be rather pointless using exceptions to handle the string module not being findable because by default string is always going to be available?

(ps taken from Learning With Python ftw)

EDIT

OK, for those wondering, as has been pointed out – my question is misguided because I failed to understand that a string obj in Python already contains the replace() method.

This was caused because I failed to do something like:

>>> dir("a string")

which would have revealed all of the methods, including ‘replace’:

So then I could have followed that up by typing:

>>> "a string".replace.__doc__

Which would have spewed out:

‘S.replace(old, new[, count]) ->
string Return a copy of string S with
all occurrences of substring old
replaced by new. If the optional
argument count is given, only the
first count occurrences are replaced.’

Now of course I knew this replace() method MUST exist somewhere, but I did not yet know how to look and discover for myself – the Python docs being frankly far too “loosely coupled” for me. Thanks for the answers – but am posting this EDIT in case any other similarly ignorant and bemused but willing Python advocate should stumble by.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T11:58:35+00:00Added an answer on May 23, 2026 at 11:58 am

    By convention, imports are placed at the top of your module/script. It makes them easier to find for you and others reading your code. There are sometimes good reasons to put imports elsewhere, say a conditional import with significant overhead.

    Another convention is not to ‘check’ that a module is loaded but instead to rely on the Python interpreter to raise a NameError exception if the object you are dereferencing is not available. Also note that multiple imports of the same module are safe as the import mechanism checks if it has been imported already.

    You may want to read http://docs.python.org/tutorial/modules.html and http://diveintopython.net/object_oriented_framework/importing_modules.html

    In this particular case, you don’t need to import anything as .replace() is a method of the string object. The string module is largely redundant (not entirely, but it is imported rarely in my experience).

    s.replace(sub, '')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working my way through some code examples and I stumbled upon this: endings
Im working my way through some C++ code and came across the following void
I'm working my way through some ASP.NET MVC reading and I have a web
I'm still working my way through some pretty basic Actionscript programming (in Flex), and
While working my way through the Android tutorials, I came across something I don't
I am working my way through some of the android developers tutorial, specifically the
I'm working my way through some tutorials. I see the Zendcasts tutorials have me
Still working my way through this program. Next task on my to-do list is
I'm working my way through K&R's 2nd edition, and I've been stumped with this
I'm currently working my way through this book: http://www1.idc.ac.il/tecs/ I'm currently on a section

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.