{
  "$type": "site.standard.document",
  "canonicalUrl": "https://rednafi.com/python/text-cropping-with-textwrap-shorten/",
  "description": "Crop text to character limits without breaking words using Python's textwrap.shorten for clean truncation with customizable placeholders.",
  "path": "/python/text-cropping-with-textwrap-shorten/",
  "publishedAt": "2022-01-06T00:00:00.000Z",
  "site": "at://did:plc:fgtm2c26vfcj74rfmeggbyqj/site.standard.publication/3mnl6f7ob462z",
  "tags": [
    "Python",
    "TIL"
  ],
  "textContent": "Problem\n\nA common interview question that I've seen goes as follows:\n\nWrite a function to crop a text corpus without breaking any word.\n\n- Take the length of the text up to which character you should trim.\n- Make sure that the cropped text doesn't have any trailing space.\n- Try to maximize the number of words you can pack in your trimmed text.\n\nYour function should look something like this:\n\nFor example, if text looks like this:\n\nand you're asked to crop it up to 9 characters, then the function crop should return:\n\nand not:\n\nor:\n\nSolution\n\nThis is quite easily solvable by using Python's [textwrap.shorten] function. The shorten\nfunction takes quite a few parameters. However, we'll only need the following ones to do our\njob:\n\n- text: str: Target text that we're going to operate on.\n- width: int : Desired width after cropping.\n- initial_indent: str: Character to use for the initial indentation. Provide empty string\n  for no initial indentation.\n- subsequent_indent: str: Character to use for the subsequent indentation. Provide empty\n  string for no subsequent indentation.\n- break_long_words: bool: Whether to break long words or not.\n- break_on_hyphens: bool: Whether to break words on hyphens or not.\n- placeholder: bool: Placeholder character. The default here is [...]. However, provide\n  an empty string if you don't want any placeholder after the cropped string. The length of\n  the placeholder is going to be included in the total length of the cropped text.\n\nWith the descriptions out of the way, let's write the crop function here:\n\nThis prints out the desired output as follows:\n\nYou can see that we achieved our goal of cropping a text corpus without breaking any word.\nTry playing around with the initial_indent, subsequent_indent, and placeholder\nparameters and see how they change the output.\n\nComplete solution with tests\n\n\n\n\n[textwrap.shorten]:\n    https://docs.python.org/3/library/textwrap.html#textwrap.shorten",
  "title": "Cropping texts in Python with 'textwrap.shorten'"
}