Skip to content

Commit

Permalink
Create string.py
Browse files Browse the repository at this point in the history
  • Loading branch information
KOSASIH authored May 10, 2024
1 parent 1f6ed4d commit a12ccc5
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/utils/string.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit a12ccc5

Please sign in to comment.