pymatgen.util.string module

This module provides utility classes for string operations.

class StringColorizer(stream)[source]

Bases: object

colours = {'blue': '\x1b[01;34m', 'cyan': '\x1b[01;36m', 'default': '', 'green': '\x1b[01;32m', 'red': '\x1b[01;31m'}
disordered_formula(disordered_struct, symbols=('x', 'y', 'z'), fmt='plain')[source]

Returns a formula of a form like AxB1-x (x=0.5) for disordered structures. Will only return a formula for disordered structures with one kind of disordered site at present.

  • disordered_struct – a disordered structure
  • symbols – a tuple of characters to use for
  • by default this is (subscripts,) –
  • if you have more than three disordered (but) –
  • more symbols will need to be added (species) –
  • fmt (str) – ‘plain’, ‘HTML’ or ‘LaTeX’

Returns (str): a disordered formula string

formula_double_format(afloat, ignore_ones=True, tol=1e-08)[source]

This function is used to make pretty formulas by formatting the amounts. Instead of Li1.0 Fe1.0 P1.0 O4.0, you get LiFePO4.

  • afloat (float) – a float
  • ignore_ones (bool) – if true, floats of 1 are ignored.
  • tol (float) – Tolerance to round to nearest int. i.e. 2.0000000001 -> 2

A string representation of the float for formulas.


Generates a HTML formatted formula, e.g. Fe2O3 is transformed to Fe<sub>2</sub>O</sub>3</sub>


Generates a LaTeX formatted formula. E.g., Fe2O3 is transformed to Fe$_{2}$O$_{3}$.

Parameters:formula (str) – Input formula.
Returns:Formula suitable for display as in LaTeX with proper subscripts.

Generates a latex formatted spacegroup. E.g., P2_1/c is converted to P2$_{1}$/c and P-1 is converted to P$overline{1}$.

Parameters:spacegroup_symbol (str) – A spacegroup symbol
Returns:A latex formatted spacegroup with proper subscripts and overlines.
str_delimited(results, header=None, delimiter='\t')[source]

Given a tuple of tuples, generate a delimited string form. >>> results = [[“a”,”b”,”c”],[“d”,”e”,”f”],[1,2,3]] >>> print(str_delimited(results,delimiter=”,”)) a,b,c d,e,f 1,2,3

  • result – 2d sequence of arbitrary types.
  • header – optional header

Aligned string output in a table-like format.


True if stream supports colours. Python cookbook, #475186

transformation_to_string(matrix, translation_vec=(0, 0, 0), components=('x', 'y', 'z'), c='', delim=', ')[source]

Convenience method. Given matrix returns string, e.g. x+2y+1/4 :param matrix :param translation_vec :param components: either (‘x’, ‘y’, ‘z’) or (‘a’, ‘b’, ‘c’) :param c: optional additional character to print (used for magmoms) :param delim: delimiter :return: xyz string


Generates a formula with unicode subscripts, e.g. Fe2O3 is transformed to Fe₂O₃. Does not support formulae with decimal points.