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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T19:30:31+00:00 2026-06-17T19:30:31+00:00

We need to display some preview text from a larger string that can be,

  • 0

We need to display some “preview text” from a larger string that can be, at most, ‘n’ characters long. Unfortunately, I couldn’t find an existing module on PyPi that handles this.

I’m hoping to do a proper solution. While my quick & dirty solution below works, it’s not that efficient — lots of constant comparisons. Does anyone have an idea on how to improve? I tried a regex, but gave up after 20 minutes.

The kludgy solution I came up with is good enough for most needs, I just know this could be done faster and more concisely – and I’d love to know how.

sample = "This is a sample string and I would like to break it down by words, without exceeding max_chars."

def clean_cut( text , max_chars ):
    rval = []
    words = text.split(' ')
    for word in words:
        len_rval = len(' '.join(rval))
        if len_rval + 1 + len(word) > max_chars :
            break
        rval.append(word)
    return ' '.join(rval)

for i in ( 15, 16, 17,30,35):
    cutdown = clean_cut( sample , i )
    print "%s | %s" % ( i , cutdown )

and output is right…

15 | This is a
16 | This is a sample
17 | This is a sample
30 | This is a sample string and I
35 | This is a sample string and I would
  • 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-17T19:30:33+00:00Added an answer on June 17, 2026 at 7:30 pm

    The following implementation may work for you

    def clean_cut(st, end):
        st += ' ' #In case end > len(st)
        return st[:st[:end + 1].rfind(' ')]
    for i in ( 15, 16, 17,30,35):
        cutdown = clean_cut( sample , i )
        print "%s | %s" % ( i , cutdown )
    

    Output

    15 | This is a
    16 | This is a sample
    17 | This is a sample
    30 | This is a sample string and I
    35 | This is a sample string and I would
    

    Note

    Compared to textwrap, this implementation is 50 times faster

    >>> stmt_ab = """
    for i in ( 15, 16, 17,30,35):
        cutdown = sample[:sample[:i + 1].rfind(' ')]
    """
    >>> stmt_mg = """
    for i in ( 15, 16, 17,30,35):
        cutdown =  textwrap.wrap(sample[:i+1],i)[0]
    """
    >>> import timeit
    >>> t1_ab = timeit.Timer(stmt=stmt_ab, setup = "from __main__ import sample")
    >>> t1_mg = timeit.Timer(stmt=stmt_mg, setup = "from __main__ import sample, textwrap")
    >>> t1_ab.timeit(10000)
    0.10367805429780219
    >>> t1_mg.timeit(10000)
    5.597085870104877
    >>> 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some text that I always need to display (like a HUD). When
I need to display some data(some text message) from a URL* (an HTML file)
I need to display some analytics to the user; however, occasionally these analytics can
Need some help assigning a mouseover event to display some icons that start out
I need your help with display of some comma separated enteries from my database.
I need to display some simple formatted text inside a small section of a
I need to display some images from a website into my tableview.I tried some
I need to display some details on DetailsView from two tables. I have table:
I need to display some images which can be scrolled horizontally. Only one image
I need to display some external data from php file to .tpl file. For

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.