0

I'm trying to store the replace function in a method to make it easier to read and work with.

Basically instead of doing str.replace(string, ' ', '_').lower() I'd prefer doing replaceandlower(string). I'm doing this to format a title to a slug (like "How's the weather today" becomes --> "hows_the_weather_today").

At this moment I have:

def replaceandlower(string):
  str.replace(string, ' ', '-').lower()

I thought that the following would work but it returns a None object type:

url = replaceandlower(title)

What I'm doing wrong?

1 Answer 1

1

You need to add a return:

def replace_lower(string):
    return string.replace(' ', '-').lower()  # Fixed the syntax for this also.

at the moment you are not actually giving anything from the function, this is equivalent to returning None.

Sign up to request clarification or add additional context in comments.

2 Comments

@user3546546 Glad I could help. If my answer was helpful to you, would you mind accepting it when the 15 minute grace period is over? You can do it by clicking on the tick below the vote counts so that it becomes green. Thank you.
@needaname Is there any reason you un-accepted the answer? Feedback would be welcome.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.