Skip to main content

How to Truncate a String in Python

String truncation is a fundamental task in many programming scenarios, from displaying abbreviated text in user interfaces to preparing data for storage or analysis.

This guide explores various methods for truncating strings in Python, providing you with a toolkit to handle different requirements effectively.

Basic String Truncation with Slicing

The simplest way to truncate a string is by using string slicing. This method extracts a portion of the string, based on specified start and stop indexes.

my_str = 'tutorialreference.com'
result = my_str[:8]
print(result) # Output: tutorial

Explanation:

  • The syntax my_str[:8] extracts the characters from the beginning of the string up to (but not including) index 8.
  • Python uses zero-based indexing, so the first character has an index of 0.

This returns the first 8 characters of the string. Note that it doesn't add ellipsis.

Adding an Ellipsis to Truncated Strings

Often, when truncating a string, it's useful to indicate that the string has been shortened. Adding an ellipsis (...) at the end is a common way to achieve this.

1. Using the Ternary Operator

The ternary operator provides a concise way to add an ellipsis conditionally.

my_str = 'tutorialreference.com'
result = my_str[:8] + '...' if len(my_str) > 8 else my_str
print(result) # Output: tutorial...

Explanation:

  • The expression my_str[:8] gets the first 8 characters.
  • The conditional if len(my_str) > 8 else my_str checks if the original string length is greater than 8.
  • If true, it adds "...", otherwise, the original string is returned, not truncated.

2. Creating a Reusable Function

For frequent use, it's beneficial to encapsulate the truncation logic in a reusable function.

def truncate_string(string, length, suffix='...'):
return string[:length] + suffix if len(string) > length else string

print(truncate_string('tutorialreference.com', 3)) # Output: tut...
print(truncate_string('tutorialreference.com', 5)) # Output: tutor...
print(truncate_string('tutorialreference.com', 7)) # Output: tutoria...

Explanation:

  • The truncate_string function takes the string, the desired length, and an optional suffix (defaulting to "...") as parameters.
  • The logic for truncation and adding an ellipsis remains the same as the ternary example.

Truncation Using Formatted String Literals (f-strings)

Formatted string literals (f-strings) allow for inline expressions and provide a concise way to truncate strings.

my_str = 'tutorialreference.com'
result = f'{my_str:.8}'
print(result) # Output: tutorial

result = f'{my_str:.8}{"..." if len(my_str) > 8 else ""}'
print(result) # Output: tutorial...

Explanation:

  • f'{my_str:.8}' truncates the string to 5 characters by using format specification mini-language.
  • The digit after the period specifies the maximum size of the string.
  • The ternary operator can also be added to conditionally add an ellipsis when a string is longer than the specified limit using {"..." if len(my_str) > 8 else ""}.

Truncating to the Last Word Using str.rsplit()

The str.rsplit() method is useful when you need to truncate a string based on word boundaries, specifically removing the last word or words.

my_str = 'tutorial reference com'
new_str = my_str.rsplit(' ', 1)[0]
print(new_str) # Output: tutorial reference

my_str = 'tutorial reference com'
result = my_str.rsplit(' ', 2)[0]
print(result) # Output: tutorial

Explanation:

  • my_str.rsplit(' ', 1) splits the string into a list, using " " as the delimiter, and performing at most one split from the right.
  • [0] accesses the first element of the list which has a string with last word removed.
  • my_str.rsplit(' ', 2) does a maximum of 2 splits, removing the last 2 words.

Advanced Truncation with textwrap.shorten()

The textwrap.shorten() method provides more advanced functionality, such as collapsing whitespace and ensuring that the placeholder is included in the final string length calculation.

import textwrap

a_string = 'tutorial reference com one two three'

new_string = textwrap.shorten(a_string, width=8, placeholder='')
print(new_string) # Output: tutorial

new_string = textwrap.shorten(a_string, width=12, placeholder='...')
print(new_string) # Output: tutorial...

new_string = textwrap.shorten(a_string, width=21, placeholder='...')
print(new_string) # Output: tutorial reference...

new_string = textwrap.shorten(a_string, width=5, placeholder='...')
print(new_string) # Output: ...

Explanation:

  • The textwrap.shorten() method takes the input string, a width argument specifying the maximum length, and a placeholder string (defaulting to [...]) as arguments.
  • If first word + placeholder > width, placeholder alone is returned.
  • It collapses whitespace before truncating, which means that multiple spaces are treated as single spaces, which is different from using slices.
  • It intelligently removes words from the end to fit within the specified width, including the placeholder length.

String Truncation Using str.format()

The str.format() method provides an alternative way to truncate strings with format specifiers.

a_string = 'tutorial reference com one two three'

new_str = '{:.8}'.format(a_string)
print(new_str) # Output: tutorial

new_str = '{:.12}'.format(a_string)
print(new_str) # Output: tutorial ref

new_str = '{:.5}'.format(a_string)
print(new_str) # Output: tutor

new_str = '{:.8}'.format(a_string) + "..." if len(a_string) > 8 else ""
print(new_str) # Output: tutorial...

Explanation:

  • The format specifier {:.N} truncates the string to N characters.
  • Ternary operators can be used with this method to add ellipsis.