diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 2ad95510..256798c8 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -65,7 +65,7 @@ If your function doesn't return anything just show how to use it. If the result To ensure your snippet isn’t refused, consider these questions: - **Does the standard library of my language provide an easy way of doing this ?** -- **Does that snippet have a real, and practical use case ?** +- **Does that snippet not have a real, and practical use case ?** - **Could it be split into separate parts to be better understood ?** If any answer is yes, then your snippet will most likely get rejected. diff --git a/snippets/python/string-manipulation/convert-string-to-ascii.md b/snippets/python/string-manipulation/convert-string-to-ascii.md deleted file mode 100644 index 61e830ba..00000000 --- a/snippets/python/string-manipulation/convert-string-to-ascii.md +++ /dev/null @@ -1,14 +0,0 @@ ---- -title: Convert String to ASCII -description: Converts a string into its ASCII representation. -author: axorax -tags: string,ascii,convert ---- - -```py -def string_to_ascii(s): - return [ord(char) for char in s] - -# Usage: -string_to_ascii('hello') # Returns: [104, 101, 108, 108, 111] -``` diff --git a/snippets/python/string-manipulation/convert-string-to-unicode.md b/snippets/python/string-manipulation/convert-string-to-unicode.md new file mode 100644 index 00000000..28d3f676 --- /dev/null +++ b/snippets/python/string-manipulation/convert-string-to-unicode.md @@ -0,0 +1,14 @@ +--- +title: Convert String to Unicode +description: Converts a string into its Unicode representation. +author: axorax +tags: string,ascii,unicode,convert +--- + +```py +def string_to_unicode(s): + return [ord(char) for char in s] + +# Usage: +string_to_unicode('hello') # Returns: [104, 101, 108, 108, 111] +``` diff --git a/snippets/python/string-manipulation/remove-specific-characters.md b/snippets/python/string-manipulation/remove-characters.md similarity index 100% rename from snippets/python/string-manipulation/remove-specific-characters.md rename to snippets/python/string-manipulation/remove-characters.md diff --git a/snippets/python/string-manipulation/reverse-string.md b/snippets/python/string-manipulation/reverse-string.md index fa045607..c5967ae9 100644 --- a/snippets/python/string-manipulation/reverse-string.md +++ b/snippets/python/string-manipulation/reverse-string.md @@ -6,8 +6,8 @@ tags: string,reverse --- ```py -def reverse_string(s): - return s[::-1] +def reverse_string(s:str) -> str: + return s[::-1] # Usage: reverse_string('hello') # Returns: 'olleh' diff --git a/snippets/python/string-manipulation/truncate-string.md b/snippets/python/string-manipulation/truncate-string.md deleted file mode 100644 index 9788ac3e..00000000 --- a/snippets/python/string-manipulation/truncate-string.md +++ /dev/null @@ -1,14 +0,0 @@ ---- -title: Truncate String -description: Truncates a string to a specified length and adds an ellipsis. -author: axorax -tags: string,truncate ---- - -```py -def truncate_string(s, length): - return s[:length] + '...' if len(s) > length else s - -# Usage: -truncate_string('This is a long string', 10) # Returns: 'This is a ...' -``` diff --git a/snippets/python/string-manipulation/truncate.md b/snippets/python/string-manipulation/truncate.md new file mode 100644 index 00000000..8f016cdd --- /dev/null +++ b/snippets/python/string-manipulation/truncate.md @@ -0,0 +1,16 @@ +--- +title: Truncate String +description: Truncates a string to a specified length and a toggleable truncation notation. +author: axorax +contributors: MinerMinerMods +tags: string,truncate +--- + +```py +def truncate(s:str, length:int, suffix:bool = True) -> str : + return (s[:length] + ("..." if suffix else "")) if len(s) > length else s + +# Usage: +truncate('This is a long string', 10) # Returns: 'This is a ...' +truncate('This is a long string', 10, False) # Returns: 'This is a ' +```