Important
This documentation covers IPython versions 6.0 and higher. Beginning with version 6.0, IPython stopped supporting compatibility with Python versions lower than 3.3 including all versions of Python 2.7.
If you are looking for an IPython version compatible with Python 2.7, please use the IPython 5.x LTS release and refer to its documentation (LTS is the long term support release).
Module: utils.text
Utilities for working with strings and text.
Inheritance diagram:

5 Classes
- class IPython.utils.text.LSString
Bases:
str
String derivative with a special access attributes.
These are normal strings, but with the special attributes:
.l (or .list) : value as list (split on newlines). .n (or .nlstr): original value (the string itself). .s (or .spstr): value as whitespace-separated string. .p (or .paths): list of path objects (requires path.py package)
Any values which require transformations are computed only once and cached.
Such strings are very useful to efficiently interact with the shell, which typically only understands whitespace-separated options for commands.
- class IPython.utils.text.SList(iterable=(), /)
Bases:
list
List derivative with a special access attributes.
These are normal lists, but with the special attributes:
.l (or .list) : value as list (the list itself).
.n (or .nlstr): value as a string, joined on newlines.
.s (or .spstr): value as a string, joined on spaces.
.p (or .paths): list of path objects (requires path.py package)
Any values which require transformations are computed only once and cached.
- fields(*fields: List[str]) List[List[str]]
Collect whitespace-separated fields from string list
Allows quick awk-like usage of string lists.
Example data (in var a, created by ‘a = !ls -l’):
-rwxrwxrwx 1 ville None 18 Dec 14 2006 ChangeLog drwxrwxrwx+ 6 ville None 0 Oct 24 18:05 IPython
a.fields(0)
is['-rwxrwxrwx', 'drwxrwxrwx+']
a.fields(1,0)
is['1 -rwxrwxrwx', '6 drwxrwxrwx+']
(note the joining by space).a.fields(-1)
is['ChangeLog', 'IPython']
IndexErrors are ignored.
Without args, fields() just split()’s the strings.
- grep(pattern: str | Callable[[Any], Match[str] | None], prune: bool = False, field: int | None = None) Self
Return all strings matching ‘pattern’ (a regex or callable)
This is case-insensitive. If prune is true, return all items NOT matching the pattern.
If field is specified, the match must occur in the specified whitespace-separated field.
Examples:
a.grep( lambda x: x.startswith('C') ) a.grep('Cha.*log', prune=1) a.grep('chm', field=-1)
- class IPython.utils.text.EvalFormatter
Bases:
Formatter
A String Formatter that allows evaluation of simple expressions.
Note that this version interprets a
:
as specifying a format string (as per standard string formatting), so if slicing is required, you must explicitly create a slice.This is to be used in templating cases, such as the parallel batch script templates, where simple arithmetic on arguments is useful.
Examples
In [1]: f = EvalFormatter() In [2]: f.format('{n//4}', n=8) Out[2]: '2' In [3]: f.format("{greeting[slice(2,4)]}", greeting="Hello") Out[3]: 'll'
- class IPython.utils.text.FullEvalFormatter
Bases:
Formatter
A String Formatter that allows evaluation of simple expressions.
Any time a format key is not found in the kwargs, it will be tried as an expression in the kwargs namespace.
Note that this version allows slicing using [1:2], so you cannot specify a format string. Use
EvalFormatter
to permit format strings.Examples
In [1]: f = FullEvalFormatter() In [2]: f.format('{n//4}', n=8) Out[2]: '2' In [3]: f.format('{list(range(5))[2:4]}') Out[3]: '[2, 3]' In [4]: f.format('{3*2}') Out[4]: '6'
- class IPython.utils.text.DollarFormatter
Bases:
FullEvalFormatter
Formatter allowing Itpl style $foo replacement, for names and attribute access only. Standard {foo} replacement also works, and allows full evaluation of its arguments.
Examples
In [1]: f = DollarFormatter() In [2]: f.format('{n//4}', n=8) Out[2]: '2' In [3]: f.format('23 * 76 is $result', result=23*76) Out[3]: '23 * 76 is 1748' In [4]: f.format('$a or {b}', a=1, b=2) Out[4]: '1 or 2'
7 Functions
- IPython.utils.text.indent(instr: str, nspaces: int = 4, ntabs: int = 0, flatten: bool = False) str
Indent a string a given number of spaces or tabstops.
indent(str, nspaces=4, ntabs=0) -> indent str by ntabs+nspaces.
- Parameters:
instr (basestring) – The string to be indented.
nspaces (int (default: 4)) – The number of spaces to be indented.
ntabs (int (default: 0)) – The number of tabs to be indented.
flatten (bool (default: False)) – Whether to scrub existing indentation. If True, all lines will be aligned to the same indentation. If False, existing indentation will be strictly increased.
- Returns:
str
- Return type:
string indented by ntabs and nspaces.
- IPython.utils.text.list_strings(arg: str | List[str]) List[str]
Always return a list of strings, given a string or list of strings as input.
Examples
In [7]: list_strings('A single string') Out[7]: ['A single string'] In [8]: list_strings(['A single string in a list']) Out[8]: ['A single string in a list'] In [9]: list_strings(['A','list','of','strings']) Out[9]: ['A', 'list', 'of', 'strings']
- IPython.utils.text.marquee(txt: str = '', width: int = 78, mark: str = '*') str
Return the input string centered in a ‘marquee’.
Examples
In [16]: marquee('A test',40) Out[16]: '**************** A test ****************' In [17]: marquee('A test',40,'-') Out[17]: '---------------- A test ----------------' In [18]: marquee('A test',40,' ') Out[18]: ' A test '
- IPython.utils.text.format_screen(strng: str) str
Format a string for screen printing.
This removes some latex-type format codes.
- IPython.utils.text.dedent(text: str) str
Equivalent of textwrap.dedent that ignores unindented first line.
This means it will still dedent strings like: ‘’’foo is a bar ‘’’
For use in wrap_paragraphs.
- IPython.utils.text.strip_email_quotes(text: str) str
Strip leading email quotation characters (‘>’).
Removes any combination of leading ‘>’ interspersed with whitespace that appears identically in all lines of the input text.
- Parameters:
text (str)
Examples
Simple uses:
In [2]: strip_email_quotes('> > text') Out[2]: 'text' In [3]: strip_email_quotes('> > text\n> > more') Out[3]: 'text\nmore'
Note how only the common prefix that appears in all lines is stripped:
In [4]: strip_email_quotes('> > text\n> > more\n> more...') Out[4]: '> text\n> more\nmore...'
So if any line has no quote marks (‘>’), then none are stripped from any of them
In [5]: strip_email_quotes('> > text\n> > more\nlast different') Out[5]: '> > text\n> > more\nlast different'
- IPython.utils.text.get_text_list(list_: List[str], last_sep: str = ' and ', sep: str = ', ', wrap_item_with: str = '') str
Return a string with a natural enumeration of items
>>> get_text_list(['a', 'b', 'c', 'd']) 'a, b, c and d' >>> get_text_list(['a', 'b', 'c'], ' or ') 'a, b or c' >>> get_text_list(['a', 'b', 'c'], ', ') 'a, b, c' >>> get_text_list(['a', 'b'], ' or ') 'a or b' >>> get_text_list(['a']) 'a' >>> get_text_list([]) '' >>> get_text_list(['a', 'b'], wrap_item_with="`") '`a` and `b`' >>> get_text_list(['a', 'b', 'c', 'd'], " = ", sep=" + ") 'a + b + c = d'