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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T23:46:01+00:00 2026-06-02T23:46:01+00:00

Given a title and a max_length, what would be the best way to shorten

  • 0

Given a title and a max_length, what would be the best way to shorten the title? The following is what I had in mind:

def shorten_title(title, max_length):
    if len(title) > max_length:
        split_title = title.split()
        length = len(title)
        n = 1
        while length > max_length:
            shortened_title = split_title[:length(split_title)-n]
            n+=1
        return shortened_title
    else:
        return title
  • 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-02T23:46:04+00:00Added an answer on June 2, 2026 at 11:46 pm
    >>> shorten_title = lambda x, y: x[:x.rindex(' ', 0, y)]
    >>> shorten_title('Shorten title to a certain length', 20)
    'Shorten title to a'
    

    That should be enough if breaking on a space is all you need. Otherwise, there are several other posts on more complex methods, like: Truncate a string without ending in the middle of a word .

    Update to address comment from okm:

    To handle edge cases, like finding no spaces before max_length, address them explicitly:

    def shorten_title2(x, y):
        if len(x) <= y:
            return x
        elif ' ' not in x[:y]:                                          
            return x[:y]
        else:
            return x[:x.rindex(' ', 0, y + 1)]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given the following models: class Post(models.Model): title = models.CharField(max_length=200) html = models.TextField() class PostTag(models.Model):
Given the following tables: shows: title | basic_ticket_price ----------------+-------------------- Inception | $3.50 Romeo &
Given the following xml: <Title> <EST> <EST_Start_Date>2009-09-21</EST_Start_Date> <EST_End_Date>2015-12-31</EST_End_Date> <EST_Version> <Vendor_ID>asdf-200130</Vendor_ID> <Master_Type_HD_SD>SD</Master_Type_HD_SD> </EST_Version> <EST_Version> <Digital_SKU>205119</Digital_SKU>
Given the following models: User.rb NetworkDepartment.rb (id, title,added_by) belongs_to :user, :foreign_key => :added_by I
Given the following xml: <language>en-US</language> <provider>VenturesLLC</provider> <video> <original_spoken_locale>en-US</original_spoken_locale> <vendor_offer_code>TEST_VENDOR</vendor_offer_code> <release_date>2011-01-15</release_date> <title>Moving Forward</title> <vendor_id>ASDF_ING_2012</vendor_id> </video>
In Django, I have the following models.py class Product(RandomPrimaryIdModel): title = models.CharField(max_length=20, blank=True, null=True)
Given my model is following: class Author(models.Model): name = models.CharField(max_length=100) description = models.TextField() class
I have the following query, which gives me the provider for a given title:
See title. Additionally, how can I tell if a given child element is only
Title pretty much sums it up. Is there a technical name given to a

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.