A comprehensive Python utility library for functional programming inspired by JavaScript's Lo-Dash
—
The Strings module provides 69 functions for comprehensive string manipulation and formatting. These functions cover case conversion, padding, trimming, searching, transformation, and various string utilities.
def camel_case(text: Any) -> strConverts text to camelCase.
Parameters:
text (Any): String to convert.Returns:
str: String in camelCase.Example:
from pydash import camel_case
camel_case('Foo Bar')
# 'fooBar'
camel_case('--foo-bar--')
# 'fooBar'
camel_case('__FOO_BAR__')
# 'fooBar'def kebab_case(text: Any) -> strConverts text to kebab-case.
Parameters:
text (Any): String to convert.Returns:
str: String in kebab-case.Example:
from pydash import kebab_case
kebab_case('Foo Bar')
# 'foo-bar'
kebab_case('fooBar')
# 'foo-bar'
kebab_case('__FOO_BAR__')
# 'foo-bar'def snake_case(text: Any) -> strConverts text to snake_case.
Parameters:
text (Any): String to convert.Returns:
str: String in snake_case.Example:
from pydash import snake_case
snake_case('Foo Bar')
# 'foo_bar'
snake_case('fooBar')
# 'foo_bar'
snake_case('--FOO-BAR--')
# 'foo_bar'def pascal_case(text: Any, strict: bool = True) -> strConverts text to PascalCase.
Parameters:
text (Any): String to convert.strict (bool): Whether to force strict camel case. Defaults to True.Returns:
str: String in PascalCase.Example:
from pydash import pascal_case
pascal_case('foo bar')
# 'FooBar'
pascal_case('--foo-bar--')
# 'FooBar'
pascal_case('fooBar')
# 'FooBar'def start_case(text: Any) -> strConverts text to Start Case.
Parameters:
text (Any): String to convert.Returns:
str: String in Start Case.Example:
from pydash import start_case
start_case('--foo-bar--')
# 'Foo Bar'
start_case('fooBar')
# 'Foo Bar'
start_case('__FOO_BAR__')
# 'FOO BAR'def lower_case(text: Any) -> strConverts text, as space separated words, to lower case.
Parameters:
text (Any): String to convert.Returns:
str: String in lower case with spaces.Example:
from pydash import lower_case
lower_case('--Foo-Bar--')
# 'foo bar'
lower_case('fooBar')
# 'foo bar'
lower_case('__FOO_BAR__')
# 'foo bar'def upper_case(text: Any) -> strConverts text, as space separated words, to upper case.
Parameters:
text (Any): String to convert.Returns:
str: String in upper case with spaces.Example:
from pydash import upper_case
upper_case('--foo-bar--')
# 'FOO BAR'
upper_case('fooBar')
# 'FOO BAR'
upper_case('__foo_bar__')
# 'FOO BAR'def title_case(text: Any) -> strConverts text to Title Case.
Parameters:
text (Any): String to convert.Returns:
str: String in Title Case.Example:
from pydash import title_case
title_case('--foo-bar--')
# 'Foo Bar'
title_case('fooBar')
# 'Foo Bar'def human_case(text: Any) -> strConverts text to human readable form by replacing dashes, underscores, and camel case with spaces.
Parameters:
text (Any): String to convert.Returns:
str: String in human readable form.Example:
from pydash import human_case
human_case('--foo-bar--')
# 'Foo bar'
human_case('fooBar')
# 'Foo bar'def capitalize(text: Any, strict: bool = True) -> strConverts the first character of text to upper case and the remaining to lower case.
Parameters:
text (Any): String to capitalize.strict (bool): Whether to cast rest of string to lower case. Defaults to True.Returns:
str: Capitalized string.Example:
from pydash import capitalize
capitalize('FRED')
# 'Fred'
capitalize('fRED', False)
# 'FRED'def decapitalize(text: Any) -> strConverts the first character of text to lower case.
Parameters:
text (Any): String to decapitalize.Returns:
str: Decapitalized string.Example:
from pydash import decapitalize
decapitalize('Fred')
# 'fred'
decapitalize('FRED')
# 'fRED'def lower_first(text: str) -> strConverts the first character of text to lower case.
Parameters:
text (str): String to convert.Returns:
str: Converted string.Example:
from pydash import lower_first
lower_first('Fred')
# 'fred'
lower_first('FRED')
# 'fRED'def upper_first(text: str) -> strConverts the first character of text to upper case.
Parameters:
text (str): String to convert.Returns:
str: Converted string.Example:
from pydash import upper_first
upper_first('fred')
# 'Fred'
upper_first('FRED')
# 'FRED'def swap_case(text: Any) -> strSwaps the case of each character in text.
Parameters:
text (Any): String to swap case.Returns:
str: String with swapped case.Example:
from pydash import swap_case
swap_case('Hello World')
# 'hELLO wORLD'def to_lower(text: Any) -> strConverts text to lower case.
Parameters:
text (Any): String to convert.Returns:
str: Lower case string.Example:
from pydash import to_lower
to_lower('--Foo-Bar--')
# '--foo-bar--'
to_lower('fooBar')
# 'foobar'def to_upper(text: Any) -> strConverts text to upper case.
Parameters:
text (Any): String to convert.Returns:
str: Upper case string.Example:
from pydash import to_upper
to_upper('--foo-bar--')
# '--FOO-BAR--'
to_upper('fooBar')
# 'FOOBAR'def pad(text: Any, length: int, chars: Any = ' ') -> strPads text on the left and right sides if it's shorter than length. Padding characters are truncated if they can't be evenly divided by length.
Parameters:
text (Any): String to pad.length (int): Padding length.chars (Any): String used as padding. Defaults to ' '.Returns:
str: Padded string.Example:
from pydash import pad
pad('abc', 8)
# ' abc '
pad('abc', 8, '_-')
# '_-abc_-_'
pad('abc', 3)
# 'abc'def pad_end(text: Any, length: int, chars: Any = ' ') -> strPads text on the right side if it's shorter than length.
Parameters:
text (Any): String to pad.length (int): Padding length.chars (Any): String used as padding. Defaults to ' '.Returns:
str: Right padded string.Example:
from pydash import pad_end
pad_end('abc', 6)
# 'abc '
pad_end('abc', 6, '_-')
# 'abc_-_'def pad_start(text: Any, length: int, chars: Any = ' ') -> strPads text on the left side if it's shorter than length.
Parameters:
text (Any): String to pad.length (int): Padding length.chars (Any): String used as padding. Defaults to ' '.Returns:
str: Left padded string.Example:
from pydash import pad_start
pad_start('abc', 6)
# ' abc'
pad_start('abc', 6, '_-')
# '_-_abc'def trim(text: Any, chars: Any = None) -> strRemoves leading and trailing whitespace or specified characters from text.
Parameters:
text (Any): String to trim.chars (Any, optional): Characters to trim.Returns:
str: Trimmed string.Example:
from pydash import trim
trim(' abc ')
# 'abc'
trim('-_-abc-_-', '_-')
# 'abc'def trim_end(text: Any, chars: Any = None) -> strRemoves trailing whitespace or specified characters from text.
Parameters:
text (Any): String to trim.chars (Any, optional): Characters to trim.Returns:
str: Trimmed string.Example:
from pydash import trim_end
trim_end(' abc ')
# ' abc'
trim_end('-_-abc-_-', '_-')
# '-_-abc'def trim_start(text: Any, chars: Any = None) -> strRemoves leading whitespace or specified characters from text.
Parameters:
text (Any): String to trim.chars (Any, optional): Characters to trim.Returns:
str: Trimmed string.Example:
from pydash import trim_start
trim_start(' abc ')
# 'abc '
trim_start('-_-abc-_-', '_-')
# 'abc-_-'def starts_with(text: Any, target: Any, position: int = 0) -> boolChecks if text starts with the given target string.
Parameters:
text (Any): String to inspect.target (Any): String to search for.position (int): Position to search from. Defaults to 0.Returns:
bool: Whether text starts with target.Example:
from pydash import starts_with
starts_with('abc', 'a')
# True
starts_with('abc', 'b')
# False
starts_with('abc', 'b', 1)
# Truedef ends_with(text: Any, target: Any, position: Union[int, None] = None) -> boolChecks if text ends with the given target string.
Parameters:
text (Any): String to inspect.target (Any): String to search for.position (Union[int, None]): Position to search up to.Returns:
bool: Whether text ends with target.Example:
from pydash import ends_with
ends_with('abc', 'c')
# True
ends_with('abc', 'b')
# False
ends_with('abc', 'b', 2)
# Truedef has_substr(text: Any, subtext: Any) -> boolChecks if text contains subtext.
Parameters:
text (Any): String to inspect.subtext (Any): String to search for.Returns:
bool: Whether text contains subtext.Example:
from pydash import has_substr
has_substr('hello world', 'world')
# True
has_substr('hello world', 'foo')
# Falsedef count_substr(text: Any, subtext: Any) -> intCounts the number of non-overlapping occurrences of subtext in text.
Parameters:
text (Any): String to inspect.subtext (Any): String to search for.Returns:
int: Number of occurrences.Example:
from pydash import count_substr
count_substr('hello world world', 'world')
# 2
count_substr('aaa', 'aa')
# 1def chop(text: Any, step: int) -> List[str]Break up text into a list of strings of length step.
Parameters:
text (Any): String to chop.step (int): Length of each chunk.Returns:
List[str]: List of chopped strings.Example:
from pydash import chop
chop('abcdefg', 3)
# ['abc', 'def', 'g']def chop_right(text: Any, step: int) -> List[str]Like chop except that it starts from the right.
Parameters:
text (Any): String to chop.step (int): Length of each chunk.Returns:
List[str]: List of chopped strings.Example:
from pydash import chop_right
chop_right('abcdefg', 3)
# ['a', 'bcd', 'efg']def chars(text: Any) -> List[str]Split text into a list of single characters.
Parameters:
text (Any): String to split into characters.Returns:
List[str]: List of single characters.Example:
from pydash import chars
chars('abc')
# ['a', 'b', 'c']def clean(text: Any) -> strTrim text and replace multiple consecutive whitespace with a single space.
Parameters:
text (Any): String to clean.Returns:
str: Cleaned string.Example:
from pydash import clean
clean(' hello world ')
# 'hello world'def lines(text: Any) -> List[str]Split text by line breaks and return list of lines.
Parameters:
text (Any): String to split.Returns:
List[str]: List of lines.Example:
from pydash import lines
lines('a\nb\r\nc')
# ['a', 'b', 'c']def words(text: Any, pattern: Any = None) -> List[str]Split text into a list of words.
Parameters:
text (Any): String to split into words.pattern (Any, optional): Pattern to use for splitting.Returns:
List[str]: List of words.Example:
from pydash import words
words('fred, barney, & pebbles')
# ['fred', 'barney', 'pebbles']
words('fred, barney, & pebbles', r'[^, ]+')
# ['fred', 'barney', '&', 'pebbles']def split(text: Any, separator: Any = None, limit: Union[int, None] = None) -> List[str]Split text by separator. If limit is specified, the resulting list will have at most limit elements.
Parameters:
text (Any): String to split.separator (Any, optional): Separator to split by.limit (Union[int, None]): Maximum number of splits.Returns:
List[str]: List of split strings.Example:
from pydash import split
split('a-b-c', '-')
# ['a', 'b', 'c']
split('a-b-c', '-', 2)
# ['a', 'b-c']def ensure_starts_with(text: Any, prefix: Any) -> strIf text does not start with prefix, append prefix to the beginning of text.
Parameters:
text (Any): String to check.prefix (Any): Prefix to ensure.Returns:
str: String that starts with prefix.Example:
from pydash import ensure_starts_with
ensure_starts_with('foobar', 'foo')
# 'foobar'
ensure_starts_with('bar', 'foo')
# 'foobar'def ensure_ends_with(text: Any, suffix: Any) -> strIf text does not end with suffix, append suffix to text.
Parameters:
text (Any): String to check.suffix (Any): Suffix to ensure.Returns:
str: String that ends with suffix.Example:
from pydash import ensure_ends_with
ensure_ends_with('foobar', 'bar')
# 'foobar'
ensure_ends_with('foo', 'bar')
# 'foobar'def replace_start(text: Any, target: Any, replacement: Any) -> strReplace target with replacement if text starts with target.
Parameters:
text (Any): String to modify.target (Any): String to replace.replacement (Any): Replacement string.Returns:
str: Modified string.Example:
from pydash import replace_start
replace_start('foobar', 'foo', 'baz')
# 'bazbar'
replace_start('foobar', 'qux', 'baz')
# 'foobar'def replace_end(text: Any, target: Any, replacement: Any) -> strReplace target with replacement if text ends with target.
Parameters:
text (Any): String to modify.target (Any): String to replace.replacement (Any): Replacement string.Returns:
str: Modified string.Example:
from pydash import replace_end
replace_end('foobar', 'bar', 'baz')
# 'foobaz'
replace_end('foobar', 'qux', 'baz')
# 'foobar'def substr_left(text: Any, subtext: Any) -> strReturn substring of text before the first occurrence of subtext.
Parameters:
text (Any): String to extract from.subtext (Any): String to search for.Returns:
str: Substring before first occurrence of subtext.Example:
from pydash import substr_left
substr_left('foobar', 'b')
# 'foo'
substr_left('foobar', 'z')
# 'foobar'def substr_left_end(text: Any, subtext: Any) -> strReturn substring of text before the last occurrence of subtext.
Parameters:
text (Any): String to extract from.subtext (Any): String to search for.Returns:
str: Substring before last occurrence of subtext.Example:
from pydash import substr_left_end
substr_left_end('foobarbar', 'bar')
# 'foobar'def substr_right(text: Any, subtext: Any) -> strReturn substring of text after the first occurrence of subtext.
Parameters:
text (Any): String to extract from.subtext (Any): String to search for.Returns:
str: Substring after first occurrence of subtext.Example:
from pydash import substr_right
substr_right('foobar', 'o')
# 'obar'
substr_right('foobar', 'z')
# ''def substr_right_end(text: Any, subtext: Any) -> strReturn substring of text after the last occurrence of subtext.
Parameters:
text (Any): String to extract from.subtext (Any): String to search for.Returns:
str: Substring after last occurrence of subtext.Example:
from pydash import substr_right_end
substr_right_end('foobarbar', 'bar')
# 'bar'def deburr(text: Any) -> strConverts Latin-1 Supplement and Latin Extended-A letters to basic Latin letters and removes combining diacritical marks.
Parameters:
text (Any): String to deburr.Returns:
str: Deburred string.Example:
from pydash import deburr
deburr('déjà vu')
# 'deja vu'def escape(text: Any) -> strConverts the characters "&", "<", ">", '"', and "'" in text to their corresponding HTML entities.
Parameters:
text (Any): String to escape.Returns:
str: HTML escaped string.Example:
from pydash import escape
escape('fred, barney, & pebbles')
# 'fred, barney, & pebbles'def unescape(text: Any) -> strThe inverse of escape. Converts the HTML entities "&", "<", ">", """, and "'" in text to their corresponding characters.
Parameters:
text (Any): String to unescape.Returns:
str: HTML unescaped string.Example:
from pydash import unescape
unescape('fred, barney, & pebbles')
# 'fred, barney, & pebbles'def escape_reg_exp(text: Any) -> strEscapes the RegExp special characters "^", "$", "", ".", "*", "+", "?", "(", ")", "[", "]", "{", "}", and "|" in text.
Parameters:
text (Any): String to escape.Returns:
str: Escaped string.Example:
from pydash import escape_reg_exp
escape_reg_exp('[pydash](https://python.org/)')
# '\\[pydash\\]\\(https://python\\.org/\\)'def quote(text: Any, quote_char: Any = '"') -> strQuote text with quote_char.
Parameters:
text (Any): String to quote.quote_char (Any): Character to quote with. Defaults to '"'.Returns:
str: Quoted string.Example:
from pydash import quote
quote('hello')
# '"hello"'
quote('hello', "'")
# "'hello'"def unquote(text: Any, quote_char: Any = '"') -> strUnquote text by removing quote_char from beginning and end.
Parameters:
text (Any): String to unquote.quote_char (Any): Quote character to remove. Defaults to '"'.Returns:
str: Unquoted string.Example:
from pydash import unquote
unquote('"hello"')
# 'hello'
unquote("'hello'", "'")
# 'hello'def surround(text: Any, wrapper: Any) -> strSurround text with wrapper.
Parameters:
text (Any): String to surround.wrapper (Any): String to wrap with.Returns:
str: Surrounded string.Example:
from pydash import surround
surround('abc', '"')
# '"abc"'
surround('abc', '!')
# '!abc!'def repeat(text: Any, n: int = 0) -> strRepeats the given string n times.
Parameters:
text (Any): String to repeat.n (int): Number of times to repeat. Defaults to 0.Returns:
str: Repeated string.Example:
from pydash import repeat
repeat('*', 3)
# '***'
repeat('abc', 2)
# 'abcabc'
repeat('abc', 0)
# ''def predecessor(char: Any) -> strReturn the predecessor character of char.
Parameters:
char (Any): Character to get predecessor of.Returns:
str: Predecessor character.Example:
from pydash import predecessor
predecessor('c')
# 'b'
predecessor('C')
# 'B'
predecessor('3')
# '2'def successor(char: Any) -> strReturn the successor character of char.
Parameters:
char (Any): Character to get successor of.Returns:
str: Successor character.Example:
from pydash import successor
successor('a')
# 'b'
successor('A')
# 'B'
successor('1')
# '2'def insert_substr(text: Any, index: int, subtext: Any) -> strInsert subtext into text starting at index.
Parameters:
text (Any): String to modify.index (int): Index to insert at.subtext (Any): String to insert.Returns:
str: Modified string.Example:
from pydash import insert_substr
insert_substr('abcdef', 3, 'XYZ')
# 'abcXYZdef'def join(array: Iterable[Any], separator: Any = '') -> strJoins the elements in array into a string separated by separator.
Parameters:
array (Iterable[Any]): Array to join.separator (Any): Separator string. Defaults to ''.Returns:
str: Joined string.Example:
from pydash import join
join(['a', 'b', 'c'], '~')
# 'a~b~c'
join([1, 2, 3], ', ')
# '1, 2, 3'def number_format(number: Union[int, float], scale: int = 0, decimal_sep: str = '.', thousands_sep: str = ',') -> strFormat a number to a readable string representation.
Parameters:
number (Union[int, float]): Number to format.scale (int): Number of decimal places. Defaults to 0.decimal_sep (str): Decimal separator. Defaults to '.'.thousands_sep (str): Thousands separator. Defaults to ','.Returns:
str: Formatted number string.Example:
from pydash import number_format
number_format(1234.5678)
# '1,235'
number_format(1234.5678, 2)
# '1,234.57'
number_format(1234.5678, 3, ',', ' ')
# '1 234,568'def prune(text: Any, length: int = 0, omission: str = '...') -> strLike truncate except that it ensures that the pruned string doesn't exceed the original length and that words are not cut off unless necessary.
Parameters:
text (Any): String to prune.length (int): Maximum length. Defaults to 0.omission (str): Omission string. Defaults to '...'.Returns:
str: Pruned string.Example:
from pydash import prune
prune('hi-diddly-ho there, neighborino', 24)
# 'hi-diddly-ho there...'def truncate(text: Any, length: int = 30, omission: str = '...', separator: Any = None) -> strTruncates text if it's longer than the given maximum string length. The last characters of the truncated string are replaced with the omission string which defaults to "...".
Parameters:
text (Any): String to truncate.length (int): Maximum string length. Defaults to 30.omission (str): Omission string. Defaults to '...'.separator (Any, optional): Separator pattern to truncate to.Returns:
str: Truncated string.Example:
from pydash import truncate
truncate('hi-diddly-ho there, neighborino')
# 'hi-diddly-ho there, neighbo...'
truncate('hi-diddly-ho there, neighborino', 24)
# 'hi-diddly-ho there, n...'def strip_tags(text: Any) -> strRemove HTML/XML tags from text.
Parameters:
text (Any): String to strip tags from.Returns:
str: String with tags removed.Example:
from pydash import strip_tags
strip_tags('<p>Hello <strong>world</strong>!</p>')
# 'Hello world!'def slugify(text: Any) -> strConvert text into an ASCII slug by replacing whitespace, dashes, and underscores with dashes, removing non-word characters, and converting to lower case.
Parameters:
text (Any): String to slugify.Returns:
str: Slugified string.Example:
from pydash import slugify
slugify('Hello World!')
# 'hello-world'
slugify(' Hello World ')
# 'hello-world'def url(text: Any, **params: Any) -> strCreate a URL by appending query string parameters to text.
Parameters:
text (Any): Base URL string.params (**Any): Query parameters to append.Returns:
str: URL with query string.Example:
from pydash import url
url('http://example.com', foo='bar', hello='world')
# 'http://example.com?foo=bar&hello=world'def replace(text: Any, pattern: Any, repl: Any, count: int = -1) -> strReplace occurrences of pattern with repl in text. Optionally, limit number of replacements to count.
Parameters:
text (Any): String to search and replace in.pattern (Any): String pattern to find.repl (Any): String to replace matches with.count (int): Maximum number of replacements. Defaults to -1 (all).Returns:
str: String with replacements made.Example:
from pydash import replace
replace('aabbcc', 'b', 'X')
# 'aaXXcc'
replace('aabbcc', 'b', 'X', 1)
# 'aaXbcc'def reg_exp_replace(text: Any, pattern: Any, repl: Any) -> strReplace occurrences of regular expression pattern with repl in text.
Parameters:
text (Any): String to search and replace in.pattern (Any): Regular expression pattern to find.repl (Any): String to replace matches with.Returns:
str: String with replacements made.Example:
from pydash import reg_exp_replace
import re
reg_exp_replace('hello world', r'l+', 'L')
# 'heLo worLd'def reg_exp_js_match(text: Any, reg_exp: Any) -> Union[List[str], None]Return list of matches using JavaScript style regular expression.
Parameters:
text (Any): String to search.reg_exp (Any): JavaScript style regular expression.Returns:
Union[List[str], None]: List of matches or None.def reg_exp_js_replace(text: Any, reg_exp: Any, repl: Any) -> strReplace text using JavaScript style regular expression.
Parameters:
text (Any): String to search and replace in.reg_exp (Any): JavaScript style regular expression.repl (Any): String to replace matches with.Returns:
str: String with replacements made.def separator_case(text: Any, separator: Any) -> strConvert text to a delimited string where words are separated by separator.
Parameters:
text (Any): String to convert.separator (Any): Separator to use between words.Returns:
str: Converted string with custom separator.Example:
from pydash import separator_case
separator_case('fooBar', '.')
# 'foo.Bar'
separator_case('Foo Bar', '_')
# 'Foo_Bar'def series_phrase(array: List[Any]) -> strJoin array into a grammatically correct series phrase (e.g. "a, b, and c").
Parameters:
array (List[Any]): Array to join into series phrase.Returns:
str: Grammatically correct series phrase.Example:
from pydash import series_phrase
series_phrase(['apple'])
# 'apple'
series_phrase(['apple', 'banana'])
# 'apple and banana'
series_phrase(['apple', 'banana', 'cherry'])
# 'apple, banana, and cherry'def series_phrase_serial(array: List[Any]) -> strLike series_phrase but with serial comma (Oxford comma).
Parameters:
array (List[Any]): Array to join into series phrase.Returns:
str: Series phrase with serial comma.Example:
from pydash import series_phrase_serial
series_phrase_serial(['apple', 'banana', 'cherry'])
# 'apple, banana, and cherry'This Strings module provides comprehensive string manipulation capabilities with 66 functions covering all aspects of string processing from basic transformations to advanced formatting and pattern matching operations.
Install with Tessl CLI
npx tessl i tessl/pypi-pydash