Welcome to Python String Utils’s documentation!¶
Contents:
-
string_utils.is_string(obj)¶ Checks if an object is a string.
Parameters: obj – Object to test. Returns: True if string, false otherwise. Return type: bool
-
string_utils.is_url(string, allowed_schemes=None)¶ Check if a string is a valid url.
Parameters: - string – String to check.
- allowed_schemes – List of valid schemes (‘http’, ‘https’, ‘ftp’...). Default to None (any scheme is valid).
Returns: True if url, false otherwise
Return type: bool
-
string_utils.is_email(string)¶ Check if a string is an email.
IMPORTANT NOTES:By design, the implementation of this checking does not follow the specification for a valid email address, but instead it’s based on real world cases in order to match more than 99% of emails and catch user mistakes. For example the percentage sign “%” is a valid sign for an email, but actually no one use it, instead if such sign is found in a string coming from user input (like a web form) is very likely that the intention was to type “5” (which is on the same key on a US keyboard).You can take a look at “IsEmailTestCase” in tests.py for further details.Parameters: string (str) – String to check. Returns: True if email, false otherwise. Return type: bool
-
string_utils.is_credit_card(string, card_type=None)¶ Checks if a string is a valid credit card number. If card type is provided then it checks that specific type, otherwise any known credit card number will be accepted.
Parameters: - string (str) – String to check.
- card_type (str) – Card type.
Can be one of these:
- VISA
- MASTERCARD
- AMERICAN_EXPRESS
- DINERS_CLUB
- DISCOVER
- JCB
or None. Default to None (any card).
Returns: True if credit card, false otherwise. Return type: bool
-
string_utils.is_camel_case(string)¶ Checks if a string is formatted as camel case. A string is considered camel case when:
- it’s composed only by letters ([a-zA-Z]) and optionally numbers ([0-9])
- it contains both lowercase and uppercase letters
- it does not start with a number
Parameters: string (str) – String to test. Returns: True for a camel case string, false otherwise. Return type: bool
-
string_utils.is_snake_case(string, separator='_')¶ Checks if a string is formatted as snake case. A string is considered snake case when:
- it’s composed only by lowercase letters ([a-z]), underscores (or provided separator) and optionally numbers ([0-9])
- it does not start/end with an underscore (or provided separator)
- it does not start with a number
Parameters: - string (str) – String to test.
- separator (str) – String to use as separator.
Returns: True for a snake case string, false otherwise.
Return type: bool
-
string_utils.is_json(string)¶ Check if a string is a valid json.
Parameters: string (str) – String to check. Returns: True if json, false otherwise Return type: bool
-
string_utils.is_uuid(string)¶ Check if a string is a valid UUID.
Parameters: string (str) – String to check. Returns: True if UUID, false otherwise Return type: bool
-
string_utils.is_ip(string)¶ Checks if a string is a valid ip.
Parameters: string (str) – String to check. Returns: True if an ip, false otherwise. Return type: bool
-
string_utils.words_count(string)¶ Returns the number of words contained into the given string.
This method is smart, it does consider only sequence of one or more letter and/or numbers as “words”, so a string like this: ”! @ # % ... []” will return zero! Moreover it is aware of punctuation, so the count for a string like “one,two,three.stop” will be 4 not 1 (even if there are no spaces in the string).
Parameters: string (str) – String to check. Returns: Number of words. Return type: int
-
string_utils.camel_case_to_snake(string, separator='_')¶ Convert a camel case string into a snake case one. (The original string is returned if is not a valid camel case string)
Parameters: - string (str) – String to convert.
- separator (str) – Sign to use as separator.
Returns: Converted string.
Return type: str
-
string_utils.snake_case_to_camel(string, upper_case_first=True, separator='_')¶ Convert a snake case string into a camel case one. (The original string is returned if is not a valid snake case string)
Parameters: - string (str) – String to convert.
- upper_case_first (bool) – True to turn the first letter into uppercase (default).
- separator (str) – Sign to use as separator (default to “_”).
Returns: Converted string
Return type: str
-
string_utils.reverse(string)¶ Returns the string reversed (“abc” -> “cba”).
Parameters: string (str) – String to revert. Returns: Reversed string. Return type: str
-
string_utils.uuid()¶ Generated an UUID string (using uuid.uuid4()).
Returns: uuid string. Return type: str
-
string_utils.shuffle(string)¶ Return a new string containing shuffled items.
Parameters: string (str) – String to shuffle Returns: Shuffled string Return type: str