From a12ccc50dc1ec38a6e8b0d2d7dc6ef44a7778d81 Mon Sep 17 00:00:00 2001 From: KOSASIH Date: Fri, 10 May 2024 10:39:33 +0700 Subject: [PATCH] Create string.py --- src/utils/string.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 src/utils/string.py diff --git a/src/utils/string.py b/src/utils/string.py new file mode 100644 index 000000000..df1b8e2e6 --- /dev/null +++ b/src/utils/string.py @@ -0,0 +1,27 @@ +def reverse_string(s: str) -> str: + """ + Returns the reverse of the given string. + """ + return s[::-1] + +def truncate_string(s: str, length: int) -> str: + """ + Returns the given string truncated to the given length. + """ + if len(s) > length: + return s[:length] + '...' + else: + return s + +def count_words(s: str) -> int: + """ + Returns the number of words in the given string. + """ + return len(s.split()) + +def count_vowels(s: str) -> int: + """ + Returns the number of vowels in the given string. + """ + vowels = 'aeiou' + return sum(1 for c in s.lower() if c in vowels)