stringr str_trunc() in R: Shorten Strings to a Fixed Width
The stringr str_trunc() function shortens a string to a fixed maximum width, replacing the cut text with an ellipsis. It is the clean way to fit long labels into tables, plots, and console output.
str_trunc(x, 20) # truncate to 20 chars, right str_trunc(x, 20, side = "left") # keep the end, cut the start str_trunc(x, 20, side = "center") # keep both ends, cut the middle str_trunc(x, 20, ellipsis = "...") # default ellipsis marker str_trunc(x, 20, ellipsis = " >") # custom ellipsis marker str_trunc(col, 30) # vectorized over a whole column
Need explanation? Read on for examples and pitfalls.
What str_trunc() does
str_trunc() caps a string at a maximum length. You give it a character vector and a width, and any string longer than that width is cut down and finished with an ellipsis so the reader knows text was removed. Strings already shorter than width are returned untouched.
It belongs to the stringr package, part of the tidyverse. The function is purely cosmetic: it never changes the underlying data, it only produces a shortened display version. That makes it ideal for axis labels, table cells, log lines, and anywhere fixed-width output keeps a layout readable.
The result is exactly 20 characters: 17 characters of text plus the 3-character ellipsis. That total-width behavior is the single most important thing to understand about the function.
str_trunc(x, 20) never returns more than 20 characters. The ellipsis is paid for out of that 20, not added on top, so the visible text is width minus nchar(ellipsis).str_trunc() syntax
The function takes one required argument and three optional ones. The full signature is short and every argument has a sensible default.
string: a character vector to truncate. Vectorized, so a whole column works.width: the maximum length of each returned string, counting the ellipsis.side: which part of the string to cut. One of"right","left", or"center".ellipsis: the marker that replaces the removed text. Defaults to"...".
Only string and width are needed in practice. The other two let you control where the cut happens and how it is signposted.
Four ways to use str_trunc()
Truncate from the right
Right truncation is the default and keeps the start of the string. Use it when the most important information sits at the beginning, which is true for most labels and titles.
Keep the end with side = "left"
Left truncation cuts the start and keeps the tail. This suits file paths, URLs, and IDs where the distinguishing part is at the end.
Keep both ends with side = "center"
Center truncation removes the middle and keeps both ends. It is useful when the start and the end both carry meaning, such as a name plus a status.
Customize the ellipsis
The ellipsis argument replaces the default three dots with any marker. Remember the marker counts toward width, so a longer marker leaves less room for text.
Truncate a whole column
str_trunc() is vectorized, so it shortens an entire column in one call. Strings shorter than width pass through unchanged, which keeps mixed-length data tidy.
mutate(label = str_trunc(name, 25)) produces compact axis text without touching the original name column, so sorting and joins still use the full values.str_trunc() vs other string functions
Several functions resize strings, but each solves a different problem. Choosing the wrong one leads to data loss or misaligned output, so match the function to the intent.
| Function | What it does | Adds an ellipsis? | Use when |
|---|---|---|---|
str_trunc() |
Caps a string at a maximum width | Yes | Showing long text in a fixed space |
str_sub() |
Extracts a substring by position | No | You need an exact character range |
str_pad() |
Extends short strings up to a width | No | Aligning values into columns |
abbreviate() |
Shortens to unique short forms | No, drops vowels | Compact unique codes or keys |
The decision rule: use str_trunc() when the goal is display and you want a visible "text was cut" signal. Reach for str_sub() when you need precise extraction and str_pad() when strings are too short rather than too long.
Series.str.slice(0, 20) cuts characters but never adds an ellipsis, so the truncation is silent. str_trunc() does both in one step.Common pitfalls
Setting width below the ellipsis length throws an error. The function cannot fit even the marker, so it stops rather than guess.
The fix is to use a width larger than nchar(ellipsis), or shorten the ellipsis itself with ellipsis = "..".
Two more traps catch beginners. First, str_trunc() only ever shortens. It will not pad a short string out to width, so it cannot align ragged data on its own; use str_pad() for that. Second, the width counts the ellipsis, so str_trunc(x, 10) shows only 7 characters of text with the default "...". If you expected 10 visible characters plus dots, raise width to 13.
Try it yourself
Try it: Truncate the cities vector to 12 characters, keeping the end of each name, and save the result to ex_short.
Click to reveal solution
Explanation: side = "left" cuts the start and keeps the tail, so "San Francisco" becomes "...Francisco". "Los Angeles" and "Boston" are already within 12 characters, so they pass through unchanged.
Related stringr functions
str_trunc() works best alongside the other string-sizing tools in stringr. Each link below covers one function in the same depth as this page.
- str_pad(): the opposite operation, padding short strings up to a width.
- str_sub(): extract a substring by start and end position.
- str_trim(): remove leading and trailing whitespace.
- str_squish(): trim ends and collapse internal whitespace.
- str_length(): measure string length before deciding a width.
For the full argument reference, see the official stringr str_trunc documentation.
FAQ
Does str_trunc() count the ellipsis in the width?
Yes. The width argument is the maximum length of the entire returned string, including the ellipsis. With the default "...", str_trunc(x, 20) returns 17 characters of text plus 3 dots. If you want a specific number of visible characters, add the ellipsis length to your target width.
What is the difference between str_trunc() and str_sub()?
str_sub() extracts an exact character range and never signals that text was removed. str_trunc() is display-focused: it caps a string at a width and appends an ellipsis so readers can see the value was shortened. Use str_sub() for precise extraction and str_trunc() for tidy, fixed-width output.
How do I truncate text from the left or middle?
Set the side argument. side = "left" keeps the end of the string and puts the ellipsis at the front, which suits paths and IDs. side = "center" keeps both ends and removes the middle. The default side = "right" keeps the start, the best choice for most labels and titles.
Can str_trunc() handle a data frame column?
Yes. str_trunc() is vectorized, so you can pass an entire character column and every value is truncated independently. Inside a dplyr pipeline, use it with mutate(), for example mutate(short_name = str_trunc(name, 25)), to add a compact display column without altering the original data.