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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:09:49+00:00 2026-05-13T14:09:49+00:00

def format_title(title): ”.join(map(lambda x: x if (x.isupper() or x.islower()) else ‘_’, title.strip())) Anything faster?

  • 0
def format_title(title):  
  ''.join(map(lambda x: x if (x.isupper() or x.islower()) else '_', title.strip()))

Anything faster?

  • 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-13T14:09:50+00:00Added an answer on May 13, 2026 at 2:09 pm

    The faster way to do it is to use str.translate()
    This is ~50 times faster than your way

    # You only need to do this once
    >>> title_trans=''.join(chr(c) if chr(c).isupper() or chr(c).islower() else '_' for c in range(256))
    
    >>> "abcde1234!@%^".translate(title_trans)
    'abcde________'
    
    # Using map+lambda
    $ python -m timeit '"".join(map(lambda x: x if (x.isupper() or x.islower()) else "_", "abcd1234!@#$".strip()))'
    10000 loops, best of 3: 21.9 usec per loop
    
    # Using str.translate
    $ python -m timeit -s 'titletrans="".join(chr(c) if chr(c).isupper() or chr(c).islower() else "_" for c in range(256))' '"abcd1234!@#$".translate(titletrans)'
    1000000 loops, best of 3: 0.422 usec per loop
    
    # Here is regex for a comparison
    $ python -m timeit -s 'import re;transre=re.compile("[\W\d]+")' 'transre.sub("_","abcd1234!@#$")'
    100000 loops, best of 3: 3.17 usec per loop
    

    Here is a version for unicode

    # coding: UTF-8
    
    def format_title_unicode_translate(title):
        return title.translate(title_unicode_trans)
    
    class TitleUnicodeTranslate(dict):
        def __missing__(self,item):
            uni = unichr(item)
            res = u"_"
            if uni.isupper() or uni.islower():
                res = uni
            self[item] = res
            return res
    title_unicode_trans=TitleUnicodeTranslate()
    
    print format_title_unicode_translate(u"Metallica Μεταλλικα")
    

    Note that the Greek letters count as upper and lower, so they are not substituted.
    If they are to be substituted, simply change the condition to

            if item<256 and (uni.isupper() or uni.islower()):
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

def record return unless @supported klasses = profile_options[:formats].map { |f| RubyProf.const_get(#{f.to_s.camelize}Printer) }.compact klasses.each do
def download_if_dne(href, filename): if os.path.isfile(filename): # print 'already downloaded:', href return False else: if
def update @album = Album.find(params[:id]) if @album.update_attributes(params[:album]) redirect_to(:action=>'list') else render(:action=>'edit') end end A Rails
def test_method [a, b, c].map {|i| yield(i) } end If I call test_method like
In my controller I have: def index @title = 'asdsadas' @kategoris = Tag.where(name like
In my controller I have: def index @title = 'asdsadas' @kategoris = Tag.where(name like
I have the following def in the client model: def cli_full_name [f_name, mi, l_name].join('
I have the following: In my controller: def gettingstarted @title = Getting Started respond_to
In a Rails controller code def create @post = Post.new(params[:post]) @post.random_hash = generate_random_hash(params[:post][:title]) if
def findDistance(): first_coord = raw_input(Enter first coordinate set (format x, y): ).split(,) second_coord =

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.