Skip to content

textcase logo

textcase

A feature-rich Python text case conversion library.

Coveralls Dependencies PyPI - Version PyPI - Downloads PyPI - Python Version PyPI - Types PyPI - Wheel AUR Version

Features#

  • Text case conversion: Convert strings between various text cases (e.g., snake_case, kebab-case, camelCase, etc.).
  • Extensible Design: Easily extend the library with custom cases and boundaries.
  • Acronym Handling: Properly detects and formats acronyms in strings (as in HTTPRequest).
  • Non-ASCII Support: Handles non-ASCII characters seamlessly (no inferences on the input language itself is made).
  • 100% Test Coverage: Comprehensive tests ensure reliability and correctness.
  • Well-Documented: Clean documentation with usage examples for easy understanding.
  • Performant: Efficient implementation without the use of regular expressions.
  • Zero Dependencies: The library has no external dependencies, making it lightweight and easy to integrate.

Installation#

pip install textcaseDone!

Usage#

You can convert strings into a case using the convert function:

from textcase import case, convert

print(convert("ronnie james dio", case.SNAKE))
print(convert("Ronnie_James_dio", case.CONSTANT))
print(convert("RONNIE_JAMES_DIO", case.KEBAB))
print(convert("RONNIE-JAMES-DIO", case.CAMEL))
print(convert("ronnie-james-dio", case.PASCAL))
print(convert("RONNIE JAMES DIO", case.LOWER))
print(convert("ronnie james dio", case.UPPER))
print(convert("ronnie-james-dio", case.TITLE))
print(convert("ronnie james dio", case.SENTENCE))
ronnie_james_dio
RONNIE_JAMES_DIO
ronnie-james-dio
ronnieJamesDio
RonnieJamesDio
ronnie james dio
RONNIE JAMES DIO
Ronnie James Dio
Ronnie james dio

By default, convert and CaseConverter.convert will split along a set of default word boundaries, that is:

  • Underscores: _,
  • Hyphens: -,
  • Spaces: ,
  • Changes in capitalization from lowercase to uppercase: aA,
  • Adjacent digits and letters: a1, 1a, A1, 1A,
  • Acronyms: AAa (as in HTTPRequest).

For more precision, you can specify boundaries to split based on the word boundaries of a particular case. For example, you can explicitly specify which boundaries will be used:

from textcase import boundary, case, convert

print(convert("2020-04-16_my_cat_cali", case.TITLE))
print(convert("2020-04-16_my_cat_cali", case.TITLE, (boundary.UNDERSCORE,)))
2020 04 16 My Cat Cali
2020-04-16 My Cat Cali

This library can detect acronyms in camel-like strings. It also ignores any leading, trailing, or duplicate delimiters:

from textcase import case, convert

print(convert("IOStream", case.SNAKE))
print(convert("myJSONParser", case.SNAKE))
print(convert("__weird--var _name-", case.SNAKE))
io_stream
my_json_parser
weird_var_name

The library also supports non-ASCII characters. However, no inferences on the input language itself is made. For example, in Dutch, the digraph "ij" is treated as two separate Unicode characters and will not be capitalized. In contrast, the character "æ" will be capitalized as expected. Also, in English the text "I THINK I DO" will be converted to "i think i do", not "I think I do". This means that the library can handle various characters:

from textcase import case, convert

print(convert("GranatÄpfel", case.KEBAB))
print(convert("ПЕРСПЕКТИВА24", case.TITLE))
print(convert("ὈΔΥΣΣΕΎΣ", case.LOWER))
granat-äpfel
Перспектива 24
ὀδυσσεύς

By default, characters followed by digits and vice-versa are considered word boundaries. In addition, any special ASCII characters (besides _ and -) are ignored:

from textcase import case, convert

print(repr(convert("E5150", case.SNAKE)))
print(repr(convert("10,000Days", case.SNAKE)))
print(repr(convert("Hello, world!", case.UPPER)))
print(repr(convert("ONE\nTWO\nTHREE", case.TITLE)))
'e_5150'
'10,000_days'
'HELLO, WORLD!'
'One\ntwo\nthree'

You can also test what case a string is in:

from textcase import case, is_case

print(is_case("css-class-name", case.KEBAB))
print(is_case("css-class-name", case.SNAKE))
print(is_case("UPPER_CASE_VAR", case.SNAKE))
True
False
False