Print very long string in Pandas dataframe
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
Pandas truncates long strings by default so tables remain readable in terminals and notebooks. That is helpful for large datasets, but it gets in the way when you need to inspect the full contents of a text column. The fix is usually to adjust display options temporarily rather than permanently changing how the whole process prints DataFrames.
Core Sections
Why Pandas Truncates Long Strings
Pandas tries to keep tabular output compact. If a column contains long text such as descriptions, JSON blobs, or log lines, the printed representation often shows only part of the value followed by an ellipsis.
Example:
By default, the full text may not be shown.
Show Full Column Width
The most direct fix is to raise or disable the maximum column width limit:
Setting None tells Pandas not to truncate column text by width.
Use a Temporary Display Context
If you do not want to change global settings for the whole process, use option_context:
This is usually the best choice in notebooks, scripts, and tests because it avoids side effects in later cells or functions.
Show One Specific Value Fully
Sometimes you do not need the whole DataFrame printed. You only need one cell:
Or for all values in a single column:
This is often clearer than trying to inspect a very wide table.
Control Row and Frame Width Too
Long strings are not the only thing affecting output. Row count and frame width also matter.
expand_frame_repr=False helps prevent line wrapping across multiple terminal lines in some environments.
Jupyter Notebook Display
In notebooks, print(df) and plain df display can behave differently. Sometimes HTML rendering still feels constrained. If needed, render a single column explicitly:
If the notebook still feels cramped, selecting only the text column can make inspection easier.
Export for External Inspection
If the strings are extremely long, printing may not be the best interface at all. Exporting can be more practical:
Or write the specific column to a text file:
This is often the right debugging move for logs, prompts, or generated text samples.
Reset Display Options When Needed
If you changed options globally and want to revert:
This is useful in shared notebooks or longer-running analysis sessions where broad display changes can surprise later code.
Choose the Right Inspection Method
A practical rule:
- use
option_contextfor temporary full display - print one cell when you need one exact value
- export when the data is too wide for comfortable terminal viewing
That keeps debugging focused instead of turning every DataFrame print into a giant wall of text.
Common Pitfalls
- Setting global display options and forgetting they affect later notebook cells or scripts.
- Trying to inspect very large text columns by printing the full DataFrame when one cell would be clearer.
- Confusing terminal width limits with Pandas truncation settings.
- Assuming notebook HTML display behaves exactly like plain
print(df). - Using full-width display on huge datasets and making output harder to read instead of easier.
Summary
- Pandas truncates long strings by default to keep table output manageable.
- Use
display.max_colwidth = Noneto show full text when needed. - Prefer
pd.option_contextfor temporary display changes. - Print individual cells or columns when that is clearer than printing the whole frame.
- Export very long text to files when console or notebook display becomes impractical.
Related reading
- Printing Lists as Tabular Data
- Probability distribution in Python
- Problems obtaining most informative features with scikit learn?
- Produce balanced mini batch with Dataset API
- Printing optional variable
- Printing Python version in output
- Programmatically determine the relative popularities of a list of items books, songs, movies, etc
- Progress indicator during pandas operations
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.