Python convert to case

convert-case 1.2.3

Convert between string cases with built-in case inference.

Ссылки проекта

Статистика

Метаданные

Лицензия: MIT License

Требует: Python >=3.6

Сопровождающие

Классификаторы

Описание проекта

Convert Case

Convert between string cases with built-in case inference.

Status

Installing

Install the package from pypi:

To experiment with the source code locally, clone the repository:

git clone https://github.com/joellefkowitz/convert-case

To install linters, formatters and test runners:

Usage

 camel  camel camel To run unit tests:
test lower upper sentence title camel snake kebab pascal
a a A A A a a a A
A a A A A a a a A
abc abc ABC Abc Abc abc abc abc Abc
ab cd ab cd AB CD Ab cd Ab Cd abCd ab_cd ab-cd AbCd
Ab cd ab cd AB CD Ab cd Ab Cd abCd ab_cd ab-cd AbCd
Ab Cd ab cd AB CD Ab cd Ab Cd abCd ab_cd ab-cd AbCd
ab_cd ab cd AB CD Ab cd Ab Cd abCd ab_cd ab-cd AbCd
ab-cd ab cd AB CD Ab cd Ab Cd abCd ab_cd ab-cd AbCd
abCd ab cd AB CD Ab cd Ab Cd abCd ab_cd ab-cd AbCd
ABCD abcd ABCD Abcd Abcd abcd abcd abcd Abcd
AbCd ab cd AB CD Ab cd Ab Cd abCd ab_cd ab-cd AbCd
ab cd ef ab cd ef AB CD EF Ab cd ef Ab Cd Ef abCdEf ab_cd_ef ab-cd-ef AbCdEf
AbCdEf ab cd ef AB CD EF Ab cd ef Ab Cd Ef abCdEf ab_cd_ef ab-cd-ef AbCdEf
ab-cd-ef ab cd ef AB CD EF Ab cd ef Ab Cd Ef abCdEf ab_cd_ef ab-cd-ef AbCdEf
Ab cd ef ab cd ef AB CD EF Ab cd ef Ab Cd Ef abCdEf ab_cd_ef ab-cd-ef AbCdEf

Numbers

Numbers are treated as letters with no specific case.

test lower upper sentence title camel snake kebab pascal
1 1 1 1 1 1 1 1 1
1bc 1bc 1BC 1bc 1bc 1bc 1bc 1bc 1bc
a1c a1c A1C A1c A1c a1c a1c a1c A1c
ab1 ab1 AB1 Ab1 Ab1 ab1 ab1 ab1 Ab1
a1 c a1 c A1 C A1 c A1 C a1C a1_c a1-c A1C
a1-c a1 c A1 C A1 c A1 C a1C a1_c a1-c A1C

Advanced

A goal of this converter is that it is deterministic. If we consider the following examples we can see that this is not simple to achieve. How should we interpret the string ‘ABC’, is it in upper case or pascal case?

test upper pascal
abc ABC Abc
a b c A B C ABC
  • To consider strings with consecutive capitals like ‘ABC’ not to be pascal case. If in this case ‘a b c’ is parsed to ‘Abc’ it would clash with parsing ‘abc’ into pascal case.
  • To store some state that remembers the string’s case before parsing. This would introduce too much complexity.
  • To prioritize parsing the string as one case unless told otherwise. We choose to pick upper case as the inferred case. The caveat here is that we will no longer be performing ’round trip’ conversion.

Not round trip conversion:

This repository's documentation is hosted on readthedocs.

Источник

Case Converter

A robust python package for transforming string cases such as Hello, world! into helloWorld (camelcase).

General usage

Delimeter behavior

If multiple delimeter characters are identified next to eachother they will be considered as a single delimeter. For example, -_ contains 2 different delimeter characters and is considered a single delimeter.

Available conversions

camelcase

delims_only : bool — Only consider delimiters as boundaries (default: False ).

pascalcase

Stripping punctuation

Punctuation is stripped when doing a case conversion. However, should you wish to keep the punctuation you can do so by passing strip_punctuation=False .

Delimeter customization

Default delimiters used to denote a token boundary.

You can pass delims to each case conversion function to specify a custom set of delimiters.

Boundaries definitions

Name Description
OnDelimeterUppercaseNext On a delimieter, upper case the following character
OnDelimeterLowercaseNext On a delimeter, lower case the following character
OnUpperPrecededByLowerAppendUpper On an upper case character followed by a lower case character, append the upper case character
OnUpperPrecededByLowerAppendLower On an upper case character preceeded by a lower case character append the lower case character
OnUpperPrecededByUpperAppendJoin On an upper case caharacter preceeded by an upper append the join character. Join characters are context dependent. Example: macro cast join character is _
OnUpperPrecededByUpperAppendCurrent On an upper case character preceeded by an upper case character append the upper case character

Contributing

  1. Write clean code.
  2. Write new tests for new use-cases.
  3. Test your code before raising a PR.
  4. Use black to format your code.

Источник

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

String case converter for python.

License

okunishinishi/python-stringcase

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Git stats

Files

Failed to load latest commit information.

README.rst

Convert string cases between camel case, pascal case, snake case etc.

import stringcase stringcase.camelcase('foo_bar_baz') # => "fooBarBaz" stringcase.camelcase('FooBarBaz') # => "fooBarBaz" stringcase.capitalcase('foo_bar_baz') # => "Foo_bar_baz" stringcase.capitalcase('FooBarBaz') # => "FooBarBaz" stringcase.constcase('foo_bar_baz') # => "FOO_BAR_BAZ" stringcase.constcase('FooBarBaz') # => "_FOO_BAR_BAZ" stringcase.lowercase('foo_bar_baz') # => "foo_bar_baz" stringcase.lowercase('FooBarBaz') # => "foobarbaz" stringcase.pascalcase('foo_bar_baz') # => "FooBarBaz" stringcase.pascalcase('FooBarBaz') # => "FooBarBaz" stringcase.pathcase('foo_bar_baz') # => "foo/bar/baz" stringcase.pathcase('FooBarBaz') # => "/foo/bar/baz" stringcase.sentencecase('foo_bar_baz') # => "Foo bar baz" stringcase.sentencecase('FooBarBaz') # => "Foo bar baz" stringcase.snakecase('foo_bar_baz') # => "foo_bar_baz" stringcase.snakecase('FooBarBaz') # => "foo_bar_baz" stringcase.spinalcase('foo_bar_baz') # => "foo-bar-baz" stringcase.spinalcase('FooBarBaz') # => "-foo-bar-baz" stringcase.titlecase('foo_bar_baz') # => "Foo Bar Baz" stringcase.titlecase('FooBarBaz') # => " Foo Bar Baz" stringcase.trimcase('foo_bar_baz') # => "foo_bar_baz" stringcase.trimcase('FooBarBaz') # => "FooBarBaz" stringcase.uppercase('foo_bar_baz') # => "FOO_BAR_BAZ" stringcase.uppercase('FooBarBaz') # => "FOOBARBAZ" stringcase.alphanumcase('_Foo., Bar') # =>'FooBar' stringcase.alphanumcase('Foo_123 Bar!') # =>'Foo123Bar'

This software is released under the MIT License.

Источник

Case Conversion

Chris Bailey

In this lesson, you’ll explore string methods that perform case conversion on the target string. These are the case conversion methods:

Here’s how to use str.capitalize() :

>>> s = 'egG BacOn SauSAGE loBSter' >>> s.capitalize() 'Egg bacon sausage lobster' >>> s = 'egg123#BACON#.' >>> s.capitalize() 'Egg123#bacon#.' 

Here’s how to use str.lower() :

>>> s = 'EGG Bacon 123 sAusAge lOBSTEr.' >>> s.lower() 'egg bacon 123 sausage lobster.' 

Here’s how to use str.swapcase() :

>>> s = 'eGG Bacon 123 sausage LOBSTER.' >>> s.swapcase() 'Egg bACON 123 SAUSAGE lobster.' 

Here’s how to use str.title() :

>>> s = 'the sun also rises' >>> s.title() 'The Sun Also Rises' >>> s = "what's happened to ted's IBM stock?" >>> s.title "What'S Happened To Ted'S Ibm Stock?" 

Here’s how to use str.upper() :

>>> s = 'EGG Bacon 123 sauSAGE lOBsTer.' >>> s.upper() 'EGG BACON 123 SAUSAGE LOBSTER.' 

Источник

Читайте также:  Python class constructor list
Оцените статью