stringutils

A functional string utility library for Python 2 and 3. Closely inspired by implementations in Haskell and PHP.

For more documentation, please see http://stringutils.readthedocs.io.

Note

This package is still in an early development stage, and it is possible that the naming and API of some functions will change.

Features

The main design goal of this library is to provide helpful string functions which complement what can already be done with str, str.format and textwrap. As such, it includes:

  • A selective set of additional string helper functions.
  • Extended versions of existing str methods as functions where useful.

Installation

Install the latest release from PyPI:

pip install stringutils

Usage

All functions are available directly off the stringutils package. You may choose to import individual functions by name, or import all.

from stringutils import reverse, unwords, words

def reverse_words(string):
    return unwords(map(reverse, words(string)))

License

The project is licensed under the MIT license.

Function reference

Assertions

stringutils.contains(string, matches)[source]

Determine if a string contains any of the given values. matches may be a single string, or a list of strings.

stringutils.contains_all(string, matches)[source]

Determine if a string contains all of the given values.

stringutils.is_whitespace(string)[source]

Determine if a string contains only whitespace characters or is empty.

Case conversion

All functions which convert to programming case formats accept strings in any of the other formats.

stringutils.camel_case(string)[source]

Convert a string identifier to camelCase.

stringutils.dashed_case(string)[source]

Convert a string identifier to dashed-case. If the string is in snake_case, capitalization of words will be preserved.

stringutils.lcfirst(string)[source]

Convert the first character of a string to lowercase.

stringutils.pascal_case(string)[source]

Convert a string identifier to PascalCase.

stringutils.snake_case(string)[source]

Convert a string identifier to snake_case. If the string is in dashed-case, capitalization of words will be preserved.

stringutils.title_case(string)[source]

Convert a string identifier to Title Case.

stringutils.ucfirst(string)[source]

Convert the first character of a string to uppercase.

Lists

stringutils.concat(strings)[source]

Concatenate a list of strings into a single string.

stringutils.join(strings, sep=', ', insertend=False)[source]

Concatenate a list of strings into a single string by a separating delimiter. If insertend is given and true, the delimiter is also included at the end of the string.

stringutils.lines(string, keepends=False)[source]

Split a string into a list of strings at newline characters. Unless keepends is given and true, the resulting strings do not have newlines included.

stringutils.split_identifier(string)[source]

Split a string identifier into a list of its subparts.

stringutils.words(string)[source]

Split a string into a list of words, which were delimited by one or more whitespace characters.

stringutils.unlines(lines, newline='\n')[source]

Join a list of lines into a single string after appending a terminating newline character to each.

stringutils.unwords(words)[source]

Join a list of words into a single string with separating spaces.

Transformations

stringutils.reverse(string)[source]

Reverse the order of the characters in a string.