String Validation¶
-
string_utils.validation.
is_string
(obj: Any) → bool¶ Checks if an object is a string.
Example:
>>> is_string('foo') # returns true >>> is_string(b'foo') # returns false
- Parameters
obj – Object to test.
- Returns
True if string, false otherwise.
-
string_utils.validation.
is_full_string
(input_string: Any) → bool¶ Check if a string is not empty (it must contains at least one non space character).
Examples:
>>> is_full_string(None) # returns false >>> is_full_string('') # returns false >>> is_full_string(' ') # returns false >>> is_full_string('hello') # returns true
- Parameters
input_string (str) – String to check.
- Returns
True if not empty, false otherwise.
-
string_utils.validation.
is_number
(input_string: str) → bool¶ Checks if a string is a valid number.
The number can be a signed (eg: +1, -2, -3.3) or unsigned (eg: 1, 2, 3.3) integer or double or use the “scientific notation” (eg: 1e5).
Examples:
>>> is_number('42') # returns true >>> is_number('19.99') # returns true >>> is_number('-9.12') # returns true >>> is_number('1e3') # returns true >>> is_number('1 2 3') # returns false
- Parameters
input_string (str) – String to check
- Returns
True if the string represents a number, false otherwise
-
string_utils.validation.
is_integer
(input_string: str) → bool¶ Checks whether the given string represents an integer or not.
An integer may be signed or unsigned or use a “scientific notation”.
Examples:
>>> is_integer('42') # returns true >>> is_integer('42.0') # returns false
- Parameters
input_string (str) – String to check
- Returns
True if integer, false otherwise
-
string_utils.validation.
is_decimal
(input_string: str) → bool¶ Checks whether the given string represents a decimal or not.
A decimal may be signed or unsigned or use a “scientific notation”.
>>> is_decimal('42.0') # returns true >>> is_decimal('42') # returns false
- Parameters
input_string (str) – String to check
- Returns
True if integer, false otherwise
-
string_utils.validation.
is_url
(input_string: Any, allowed_schemes: Optional[List[str]] = None) → bool¶ Check if a string is a valid url.
Examples:
>>> is_url('http://www.mysite.com') # returns true >>> is_url('https://mysite.com') # returns true >>> is_url('.mysite.com') # returns false
- Parameters
input_string (str) – String to check.
allowed_schemes (Optional[List[str]]) – List of valid schemes (‘http’, ‘https’, ‘ftp’…). Default to None (any scheme is valid).
- Returns
True if url, false otherwise
-
string_utils.validation.
is_email
(input_string: Any) → bool¶ Check if a string is a valid email.
Reference: https://tools.ietf.org/html/rfc3696#section-3
Examples:
>>> is_email('my.email@the-provider.com') # returns true >>> is_email('@gmail.com') # returns false
- Parameters
input_string (str) – String to check.
- Returns
True if email, false otherwise.
-
string_utils.validation.
is_credit_card
(input_string: Any, card_type: str = None) → bool¶ Checks if a string is a valid credit card number. If card type is provided then it checks against that specific type only, otherwise any known credit card number will be accepted.
Supported card types are the following:
VISA
MASTERCARD
AMERICAN_EXPRESS
DINERS_CLUB
DISCOVER
JCB
- Parameters
input_string (str) – String to check.
card_type (str) – Card type. Default to None (any card).
- Returns
True if credit card, false otherwise.
-
string_utils.validation.
is_camel_case
(input_string: Any) → bool¶ 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
Examples:
>>> is_camel_case('MyString') # returns true >>> is_camel_case('mystring') # returns false
- Parameters
input_string (str) – String to test.
- Returns
True for a camel case string, false otherwise.
-
string_utils.validation.
is_snake_case
(input_string: Any, separator: str = '_') → bool¶ Checks if a string is formatted as “snake case”.
A string is considered snake case when:
it’s composed only by lowercase/uppercase letters and digits
it contains at least one underscore (or provided separator)
it does not start with a number
Examples:
>>> is_snake_case('foo_bar_baz') # returns true >>> is_snake_case('foo') # returns false
- Parameters
input_string (str) – String to test.
separator (str) – String to use as separator.
- Returns
True for a snake case string, false otherwise.
-
string_utils.validation.
is_json
(input_string: Any) → bool¶ Check if a string is a valid json.
Examples:
>>> is_json('{"name": "Peter"}') # returns true >>> is_json('[1, 2, 3]') # returns true >>> is_json('{nope}') # returns false
- Parameters
input_string (str) – String to check.
- Returns
True if json, false otherwise
-
string_utils.validation.
is_uuid
(input_string: Any, allow_hex: bool = False) → bool¶ Check if a string is a valid UUID.
Example:
>>> is_uuid('6f8aa2f9-686c-4ac3-8766-5712354a04cf') # returns true >>> is_uuid('6f8aa2f9686c4ac387665712354a04cf') # returns false >>> is_uuid('6f8aa2f9686c4ac387665712354a04cf', allow_hex=True) # returns true
- Parameters
input_string (str) – String to check.
allow_hex (bool) – True to allow UUID hex representation as valid, false otherwise (default)
- Returns
True if UUID, false otherwise
-
string_utils.validation.
is_ip_v4
(input_string: Any) → bool¶ Checks if a string is a valid ip v4.
Examples:
>>> is_ip_v4('255.200.100.75') # returns true >>> is_ip_v4('nope') # returns false (not an ip) >>> is_ip_v4('255.200.100.999') # returns false (999 is out of range)
- Parameters
input_string (str) – String to check.
- Returns
True if an ip v4, false otherwise.
-
string_utils.validation.
is_ip_v6
(input_string: Any) → bool¶ Checks if a string is a valid ip v6.
Examples:
>>> is_ip_v6('2001:db8:85a3:0000:0000:8a2e:370:7334') # returns true >>> is_ip_v6('2001:db8:85a3:0000:0000:8a2e:370:?') # returns false (invalid "?")
- Parameters
input_string (str) – String to check.
- Returns
True if a v6 ip, false otherwise.
-
string_utils.validation.
is_ip
(input_string: Any) → bool¶ Checks if a string is a valid ip (either v4 or v6).
Examples:
>>> is_ip('255.200.100.75') # returns true >>> is_ip('2001:db8:85a3:0000:0000:8a2e:370:7334') # returns true >>> is_ip('1.2.3') # returns false
- Parameters
input_string (str) – String to check.
- Returns
True if an ip, false otherwise.
-
string_utils.validation.
is_isbn_10
(input_string: str, normalize: bool = True) → bool¶ Checks if the given string represents a valid ISBN 10 (International Standard Book Number). By default hyphens in the string are ignored, so digits can be separated in different ways, by calling this function with normalize=False only digit-only strings will pass the validation.
Examples:
>>> is_isbn_10('1506715214') # returns true >>> is_isbn_10('150-6715214') # returns true >>> is_isbn_10('150-6715214', normalize=False) # returns false
- Parameters
input_string – String to check.
normalize – True to ignore hyphens (“-“) in the string (default), false otherwise.
- Returns
True if valid ISBN 10, false otherwise.
-
string_utils.validation.
is_isbn_13
(input_string: str, normalize: bool = True) → bool¶ Checks if the given string represents a valid ISBN 13 (International Standard Book Number). By default hyphens in the string are ignored, so digits can be separated in different ways, by calling this function with normalize=False only digit-only strings will pass the validation.
Examples:
>>> is_isbn_13('9780312498580') # returns true >>> is_isbn_13('978-0312498580') # returns true >>> is_isbn_13('978-0312498580', normalize=False) # returns false
- Parameters
input_string – String to check.
normalize – True to ignore hyphens (“-“) in the string (default), false otherwise.
- Returns
True if valid ISBN 13, false otherwise.
-
string_utils.validation.
is_isbn
(input_string: str, normalize: bool = True) → bool¶ Checks if the given string represents a valid ISBN (International Standard Book Number). By default hyphens in the string are ignored, so digits can be separated in different ways, by calling this function with normalize=False only digit-only strings will pass the validation.
Examples:
>>> is_isbn('9780312498580') # returns true >>> is_isbn('1506715214') # returns true
- Parameters
input_string – String to check.
normalize – True to ignore hyphens (“-“) in the string (default), false otherwise.
- Returns
True if valid ISBN (10 or 13), false otherwise.
-
string_utils.validation.
is_palindrome
(input_string: Any, ignore_spaces: bool = False, ignore_case: bool = False) → bool¶ Checks if the string is a palindrome (https://en.wikipedia.org/wiki/Palindrome).
Examples:
>>> is_palindrome('LOL') # returns true >>> is_palindrome('Lol') # returns false >>> is_palindrome('Lol', ignore_case=True) # returns true >>> is_palindrome('ROTFL') # returns false
- Parameters
input_string (str) – String to check.
ignore_spaces (bool) – False if white spaces matter (default), true otherwise.
ignore_case (bool) – False if char case matters (default), true otherwise.
- Returns
True if the string is a palindrome (like “otto”, or “i topi non avevano nipoti” if strict=False), False otherwise
-
string_utils.validation.
is_pangram
(input_string: Any) → bool¶ Checks if the string is a pangram (https://en.wikipedia.org/wiki/Pangram).
Examples:
>>> is_pangram('The quick brown fox jumps over the lazy dog') # returns true >>> is_pangram('hello world') # returns false
- Parameters
input_string (str) – String to check.
- Returns
True if the string is a pangram, False otherwise.
-
string_utils.validation.
is_isogram
(input_string: Any) → bool¶ Checks if the string is an isogram (https://en.wikipedia.org/wiki/Isogram).
Examples:
>>> is_isogram('dermatoglyphics') # returns true >>> is_isogram('hello') # returns false
- Parameters
input_string (str) – String to check.
- Returns
True if isogram, false otherwise.
-
string_utils.validation.
is_slug
(input_string: Any, separator: str = '-') → bool¶ Checks if a given string is a slug (as created by slugify()).
Examples:
>>> is_slug('my-blog-post-title') # returns true >>> is_slug('My blog post title') # returns false
- Parameters
input_string (str) – String to check.
separator (str) – Join sign used by the slug.
- Returns
True if slug, false otherwise.
-
string_utils.validation.
contains_html
(input_string: str) → bool¶ Checks if the given string contains HTML/XML tags.
By design, this function matches ANY type of tag, so don’t expect to use it as an HTML validator, its goal is to detect “malicious” or undesired tags in the text.
Examples:
>>> contains_html('my string is <strong>bold</strong>') # returns true >>> contains_html('my string is not bold') # returns false
- Parameters
input_string (str) – Text to check
- Returns
True if string contains html, false otherwise.
-
string_utils.validation.
words_count
(input_string: str) → int¶ 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).
Examples:
>>> words_count('hello world') # returns 2 >>> words_count('one,two,three.stop') # returns 4
- Parameters
input_string (str) – String to check.
- Returns
Number of words.