pymatgen.util.string module

This module provides utility classes for string operations.

class Stringify[source]

Bases: object

Mix-in class for string formatting, e.g. superscripting numbers and symbols or superscripting.

STRING_MODE = 'SUBSCRIPT'[source]
to_html_string() → str[source]

Generates a HTML formatted string. This uses the output from to_latex_string to generate a HTML output. :return: HTML formatted string.

to_latex_string() → str[source]

Generates a LaTeX formatted string. The mode is set by the class variable STRING_MODE, which defaults to “SUBSCRIPT”. E.g., Fe2O3 is transformed to Fe$_{2}$O$_{3}$. Setting STRING_MODE to “SUPERSCRIPT” creates superscript, e.g., Fe2+ becomes Fe^{2+}. The initial string is obtained from the class’s __str__ method.

Returns

String for display as in LaTeX with proper superscripts and subscripts.

to_pretty_string() → str[source]
Returns

A pretty string representation. By default, the __str__ output is used, but this method can be overridden if a different representation from default is desired.

to_unicode_string()[source]
Returns

Unicode string with proper sub and superscripts. Note that this works only with systems where the sub and superscripts are pure integers.

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.

Parameters
  • 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.

Parameters
  • 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

Returns

A string representation of the float for formulas.

htmlify(formula)[source]

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

Note that Composition now has a to_html_string() method that may be used instead.

Parameters

formula

Returns

latexify(formula)[source]

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

Note that Composition now has a to_latex_string() method that may be used instead.

Parameters

formula (str) – Input formula.

Returns

Formula suitable for display as in LaTeX with proper subscripts.

latexify_spacegroup(spacegroup_symbol)[source]

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}$.

Note that SymmetryGroup now has a to_latex_string() method that may be called instead.

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

Parameters
  • result – 2d sequence of arbitrary types.

  • header – optional header

Returns

Aligned string output in a table-like format.

stream_has_colours(stream)[source]

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

unicodeify(formula)[source]

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

Note that Composition now has a to_unicode_string() method that may be used instead.

Parameters

formula

Returns

unicodeify_spacegroup(spacegroup_symbol)[source]

Generates a unicode formatted spacegroup. E.g., P2$_{1}$/c is converted to P2₁/c and P$\overline{1}$ is converted to P̅1.

Note that SymmetryGroup now has a to_unicode_string() method that may be called instead.

Parameters

spacegroup_symbol (str) – A spacegroup symbol as LaTeX

Returns

A unicode spacegroup with proper subscripts and overlines.

unicodeify_species(specie_string)[source]

Generates a unicode formatted species string, with appropriate superscripts for oxidation states.

Note that Species now has a to_unicode_string() method that may be used instead.

Parameters

specie_string (str) – Species string, e.g. O2-

Returns

Species string, e.g. O²⁻