monty.string module

Useful additional string functions.

boxed(msg, ch='=', pad=5)[source]

Returns a string in a box

Parameters
  • msg – Input string.

  • ch – Character used to form the box.

  • pad – Number of characters ch added before and after msg.

>>> print(boxed("hello", ch="*", pad=2))
***********
** hello **
***********
indent(lines, amount, ch=' ')[source]

Indent the lines in a string by padding each one with proper number of pad characters

is_string(s)[source]

True if s behaves like a string (duck typing test).

list_strings(arg)[source]

Always return a list of strings, given a string or list of strings as input.

Examples

>>> list_strings('A single string')
['A single string']
>>> list_strings(['A single string in a list'])
['A single string in a list']
>>> list_strings(['A','list','of','strings'])
['A', 'list', 'of', 'strings']
make_banner(s, width=78, mark='*')[source]
Parameters
  • s – String

  • width – Width of banner. Defaults to 78.

  • mark – The mark used to create the banner.

Returns

Banner string.

marquee(text='', width=78, mark='*')[source]

Return the input string centered in a ‘marquee’.

Parameters
  • text (str) – Input string

  • width (int) – Width of final output string.

  • mark (str) – Character used to fill string.

Examples

>>> marquee('A test', width=40)
'**************** A test ****************'
>>> marquee('A test', width=40, mark='-')
'---------------- A test ----------------'

marquee(‘A test’,40, ‘ ‘) ‘ A test ‘

remove_non_ascii(s)[source]

Remove non-ascii characters in a file. Needed when support for non-ASCII is not available.

Parameters

s (str) – Input string

Returns

String with all non-ascii characters removed.

unicode2str(s)[source]

Forces a unicode to a string in Python 2, but transparently handles Python 3.

Parameters

s (str/unicode) – Input string / unicode.

Returns

str in Python 2. Unchanged otherwise.