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 7909473
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T12:26:01+00:00 2026-06-03T12:26:01+00:00

I was building a bit of code that would trim off any non-digit entries

  • 0

I was building a bit of code that would trim off any non-digit entries from the start and end of a string, I had a very confusing issue with the following bit of code:

def String_Trim(Raw_String):
        if Raw_String[0].isdigit() == False:
            New_String = Raw_String[1:]
            String_Trim(New_String)
        elif Raw_String[-1].isdigit() == False:
            New_String = Raw_String[:-1]
            String_Trim(New_String)
        else:
            print Raw_String
            return Raw_String

print(String_Trim('ab19fsd'))

The initial printing of Raw_String works fine and displays the value that I want (19), but for some reason, the last line trying to print the return value of String_Trim returns a None. What exactly is python doing here and how can I fix it? Any other comments about improving my code would also be greatly appreciated.

  • 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-06-03T12:26:02+00:00Added an answer on June 3, 2026 at 12:26 pm

    Use regex for this. Recursion for trimming a string is really not a good idea:

    import re
    
    def trim_string(string):
      return re.sub(r'^([^0-9]+)(.*?)([^0-9]+)$', r'\2', string)
    

    To break it down, the regex (r'^([^0-9]+)(.*?)([^0-9]+)$') is like so:

    • ^ matches the start of a string.
    • ([^0-9]+) matches a group of consecutive non-digit characters.
    • (.*?) matches a group of stuff (non-greedy).
    • ([^0-9]+) matches another group of consecutive non-digit characters.
    • $ matches the end of the string.

    The replacement string, r'\2', just says to replace the matched string with only the second group, which is the stuff between the two groups of non-digit characters.


    But if you’re really sure you want to use your existing solution, you need to understand how recursion actually works. When you call return foo, the function returns foo as its output. If you don’t call return, you return None automatically.

    That being said, you need to return in every case of the recursion process, not just at the end:

    def String_Trim(Raw_String):
        if Raw_String[0].isdigit() == False:
            New_String = Raw_String[1:]
            return String_Trim(New_String)
        elif Raw_String[-1].isdigit() == False:
            New_String = Raw_String[:-1]
            return String_Trim(New_String)
        else:
            print Raw_String
            return Raw_String
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm building a C# application that loads a 32-bit COM dll. The compiled application
Okay, this one seems to me a bit weird. Any help would be greatly
I'm building a rails app that takes information about products from an XML datafeed
I have quite a bit of code that I find myself using over and
Would I expect to see any performance gain by building my native C++ Client
I have a bit of a problem building my project. I'm getting the bellow
a bit of a PHP / MySQL newbie here... I've been building a PHP-based
I'm building a CakePHP site for a client and I've got a bit of
After building the application GUI, I realised it was a little bit too complex
I'm building a database in django but I've run into a bit of an

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.