66
77__all__ = [
88'is_string' ,
9+ 'is_full_string' ,
910'is_number' ,
1011'is_integer' ,
1112'is_decimal' ,
12- 'is_full_string' ,
1313'is_url' ,
1414'is_email' ,
1515'is_credit_card' ,
@@ -83,6 +83,11 @@ def is_string(obj: Any) -> bool:
8383"""
8484 Checks if an object is a string.
8585
86+ *Example:*
87+
88+ >>> is_string('foo') # returns true
89+ >>> is_string(b'foo') # returns false
90+
8691 :param obj: Object to test.
8792 :return: True if string, false otherwise.
8893 """
@@ -423,43 +428,61 @@ def is_ip(input_string: Any) -> bool:
423428return is_ip_v6 (input_string )or is_ip_v4 (input_string )
424429
425430
426- def is_palindrome (input_string :Any ,strict :bool = True )-> bool :
431+ def is_palindrome (input_string :Any ,ignore_spaces :bool = False , ignore_case : bool = False )-> bool :
427432"""
428433 Checks if the string is a palindrome (https://en.wikipedia.org/wiki/Palindrome).
429434
435+ *Examples:*
436+
437+ >>> is_palindrome('LOL') # returns true
438+ >>> is_palindrome('Lol') # returns false
439+ >>> is_palindrome('Lol', ignore_case=True) # returns true
440+ >>> is_palindrome('ROTFL') # returns false
441+
430442 :param input_string: String to check.
431443 :type input_string: str
432- :param strict: True if white spaces matter (default), false otherwise.
433- :type strict: bool
444+ :param ignore_spaces: False if white spaces matter (default), true otherwise.
445+ :type ignore_spaces: bool
446+ :param ignore_case: False if char case matters (default), true otherwise.
447+ :type ignore_case: bool
434448 :return: True if the string is a palindrome (like "otto", or "i topi non avevano nipoti" if strict=False),\
435449 False otherwise
436450 """
437- if is_full_string (input_string ):
438- if strict :
439- string_len = len (input_string )
451+ if not is_full_string (input_string ):
452+ return False
440453
441- # Traverse the string one char at step, and for each step compares the
442- # "head_char" (the one on the left of the string) to the "tail_char" (the one on the right).
443- # In this way we avoid to manipulate the whole string in advance if not necessary and provide a faster
444- # algorithm which can scale very well for long strings.
445- for index in range (string_len ):
446- head_char = input_string [index ]
447- tail_char = input_string [string_len - index - 1 ]
454+ if ignore_spaces :
455+ input_string = SPACES_RE .sub ('' ,input_string )
448456
449- if head_char != tail_char :
450- return False
457+ string_len = len (input_string )
451458
452- return True
459+ # Traverse the string one char at step, and for each step compares the
460+ # "head_char" (the one on the left of the string) to the "tail_char" (the one on the right).
461+ # In this way we avoid to manipulate the whole string in advance if not necessary and provide a faster
462+ # algorithm which can scale very well for long strings.
463+ for index in range (string_len ):
464+ head_char = input_string [index ]
465+ tail_char = input_string [string_len - index - 1 ]
453466
454- return is_palindrome (SPACES_RE .sub ('' ,input_string ))
467+ if ignore_case :
468+ head_char = head_char .lower ()
469+ tail_char = tail_char .lower ()
455470
456- return False
471+ if head_char != tail_char :
472+ return False
473+
474+ return True
457475
458476
459477def is_pangram (input_string :Any )-> bool :
460478"""
461479 Checks if the string is a pangram (https://en.wikipedia.org/wiki/Pangram).
462480
481+ *Examples:*
482+
483+ >>> is_pangram('The quick brown fox jumps over the lazy dog') # returns true
484+ >>> is_pangram('hello world') # returns false
485+
463486 :param input_string: String to check.
464487 :type input_string: str
465488 :return: True if the string is a pangram, False otherwise.
@@ -474,6 +497,11 @@ def is_isogram(input_string: Any) -> bool:
474497"""
475498 Checks if the string is an isogram (https://en.wikipedia.org/wiki/Isogram).
476499
500+ *Examples:*
501+
502+ >>> is_isogram('dermatoglyphics') # returns true
503+ >>> is_isogram('hello') # returns false
504+
477505 :param input_string: String to check.
478506 :type input_string: str
479507 :return: True if isogram, false otherwise.
@@ -485,6 +513,11 @@ def is_slug(input_string: Any, sign: str = '-') -> bool:
485513"""
486514 Checks if a given string is a slug.
487515
516+ *Examples:*
517+
518+ >>> is_slug('my-blog-post-title') # returns true
519+ >>> is_slug('My blog post title') # returns false
520+
488521 :param input_string: String to check.
489522 :type input_string: str
490523 :param sign: Join sign used by the slug.
@@ -505,6 +538,11 @@ def contains_html(input_string: str) -> bool:
505538 By design, this function is very permissive regarding what to consider html code, don't expect to use it
506539 as an html validator, its goal is to detect "malicious" or undesired html tags in the text.
507540
541+ *Examples:*
542+
543+ >>> contains_html('my string is <strong>bold</strong>') # returns true
544+ >>> contains_html('my string is not bold') # returns false
545+
508546 :param input_string: Text to check
509547 :type input_string: str
510548 :return: True if string contains html, false otherwise.
@@ -524,6 +562,11 @@ def words_count(input_string: str) -> int:
524562 Moreover it is aware of punctuation, so the count for a string like "one,two,three.stop"
525563 will be 4 not 1 (even if there are no spaces in the string).
526564
565+ *Examples:*
566+
567+ >>> words_count('hello world') # returns 2
568+ >>> words_count('one,two,three') # returns 3 (no need for spaces, punctuation is recognized!)
569+
527570 :param input_string: String to check.
528571 :type input_string: str
529572 :return: Number of words.
@@ -540,6 +583,12 @@ def is_isbn_10(input_string: str, normalize: bool = True) -> bool:
540583 By default hyphens in the string are ignored, so digits can be separated in different ways, by calling this
541584 function with `normalize=False` only digit-only strings will pass the validation.
542585
586+ *Examples:*
587+
588+ >>> is_isbn_10('1506715214') # returns true
589+ >>> is_isbn_10('150-6715214') # returns true
590+ >>> is_isbn_10('150-6715214', normalize=False) # returns false
591+
543592 :param input_string: String to check.
544593 :param normalize: True to ignore hyphens ("-") in the string (default), false otherwise.
545594 :return: True if valid ISBN 10, false otherwise.
@@ -554,6 +603,12 @@ def is_isbn_13(input_string: str, normalize: bool = True) -> bool:
554603 By default hyphens in the string are ignored, so digits can be separated in different ways, by calling this
555604 function with `normalize=False` only digit-only strings will pass the validation.
556605
606+ *Examples:*
607+
608+ >>> is_isbn_13('9780312498580') # returns true
609+ >>> is_isbn_13('978-0312498580') # returns true
610+ >>> is_isbn_13('978-0312498580', normalize=False) # returns false
611+
557612 :param input_string: String to check.
558613 :param normalize: True to ignore hyphens ("-") in the string (default), false otherwise.
559614 :return: True if valid ISBN 13, false otherwise.
@@ -568,6 +623,11 @@ def is_isbn(input_string: str, normalize: bool = True) -> bool:
568623 By default hyphens in the string are ignored, so digits can be separated in different ways, by calling this
569624 function with `normalize=False` only digit-only strings will pass the validation.
570625
626+ *Examples:*
627+
628+ >>> is_isbn('9780312498580') # returns true
629+ >>> is_isbn('1506715214') # returns true
630+
571631 :param input_string: String to check.
572632 :param normalize: True to ignore hyphens ("-") in the string (default), false otherwise.
573633 :return: True if valid ISBN (10 or 13), false otherwise.