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

  • Home
  • SEARCH
  • 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 152963
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T09:43:01+00:00 2026-05-11T09:43:01+00:00

I have a simple task I need to perform in Python, which is to

  • 0

I have a simple task I need to perform in Python, which is to convert a string to all lowercase and strip out all non-ascii non-alpha characters.

For example:

'This is a Test' -> 'thisisatest' 'A235th@#$&( er Ra{}|?>ndom' -> 'atherrandom' 

I have a simple function to do this:

import string import sys  def strip_string_to_lowercase(s):     tmpStr = s.lower().strip()     retStrList = []     for x in tmpStr:         if x in string.ascii_lowercase:             retStrList.append(x)      return ''.join(retStrList) 

But I cannot help thinking there is a more efficient, or more elegant, way.

Thanks!


Edit:

Thanks to all those that answered. I learned, and in some cases re-learned, a good deal of python.

  • 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. 2026-05-11T09:43:02+00:00Added an answer on May 11, 2026 at 9:43 am

    Another solution (not that pythonic, but very fast) is to use string.translate – though note that this will not work for unicode. It’s also worth noting that you can speed up Dana’s code by moving the characters into a set (which looks up by hash, rather than performing a linear search each time). Here are the timings I get for various of the solutions given:

    import string, re, timeit  # Precomputed values (for str_join_set and translate)  letter_set = frozenset(string.ascii_lowercase + string.ascii_uppercase) tab = string.maketrans(string.ascii_lowercase + string.ascii_uppercase,                        string.ascii_lowercase * 2) deletions = ''.join(ch for ch in map(chr,range(256)) if ch not in letter_set)  s='A235th@#$&( er Ra{}|?>ndom'  # From unwind's filter approach def test_filter(s):     return filter(lambda x: x in string.ascii_lowercase, s.lower())  # using set instead (and contains) def test_filter_set(s):     return filter(letter_set.__contains__, s).lower()  # Tomalak's solution def test_regex(s):     return re.sub('[^a-z]', '', s.lower())  # Dana's def test_str_join(s):     return ''.join(c for c in s.lower() if c in string.ascii_lowercase)  # Modified to use a set. def test_str_join_set(s):     return ''.join(c for c in s.lower() if c in letter_set)  # Translate approach. def test_translate(s):     return string.translate(s, tab, deletions)   for test in sorted(globals()):     if test.startswith('test_'):         assert globals()[test](s)=='atherrandom'         print '%30s : %s' % (test, timeit.Timer('f(s)',                'from __main__ import %s as f, s' % test).timeit(200000)) 

    This gives me:

                   test_filter : 2.57138351271            test_filter_set : 0.981806765698                 test_regex : 3.10069885233              test_str_join : 2.87172979743          test_str_join_set : 2.43197956381             test_translate : 0.335367566218 

    [Edit] Updated with filter solutions as well. (Note that using set.__contains__ makes a big difference here, as it avoids making an extra function call for the lambda.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 76k
  • Answers 76k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer You need to use a CASE WHEN statement within queries… May 11, 2026 at 3:09 pm
  • added an answer The second approach is more idiomatic, and expressive. It is… May 11, 2026 at 3:09 pm
  • added an answer No. JavaScript may be one of the klunkiest languages ever,… May 11, 2026 at 3:09 pm

Related Questions

I have a task that I need to perform for a friend as a
I have a data flow task that extracts data from one database and inserts
I am developing a system as an aid to musicians performing transcription. The aim
Let's say one has a class that performs a certain type of task. And

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.