From aaf95ef63f12d5b63ef41869312f4af93593595c Mon Sep 17 00:00:00 2001 From: Alessandro Capialbi Date: Wed, 3 Sep 2025 11:10:15 +0200 Subject: [PATCH 01/11] docs: init metadata --- .../terms/userstring/userstring.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 content/python/concepts/collections-module/terms/userstring/userstring.md diff --git a/content/python/concepts/collections-module/terms/userstring/userstring.md b/content/python/concepts/collections-module/terms/userstring/userstring.md new file mode 100644 index 00000000000..0a620f7f5b1 --- /dev/null +++ b/content/python/concepts/collections-module/terms/userstring/userstring.md @@ -0,0 +1,12 @@ +--- +Title: 'collections.UserString' +Description: 'A custom class that wraps around string objects.' +Subjects: + - 'Computer Science' +Tags: + - 'Strings' + - 'Data Types' +CatalogContent: + - 'learn-python-3' + - 'paths/computer-science' +--- \ No newline at end of file From 515ad01dafe1630ad48fd6704f0b12c3409dc73b Mon Sep 17 00:00:00 2001 From: Alessandro Capialbi Date: Wed, 3 Sep 2025 11:34:02 +0200 Subject: [PATCH 02/11] docs: brief description --- .../collections-module/terms/userstring/userstring.md | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/content/python/concepts/collections-module/terms/userstring/userstring.md b/content/python/concepts/collections-module/terms/userstring/userstring.md index 0a620f7f5b1..dd2691c7e99 100644 --- a/content/python/concepts/collections-module/terms/userstring/userstring.md +++ b/content/python/concepts/collections-module/terms/userstring/userstring.md @@ -1,6 +1,6 @@ --- Title: 'collections.UserString' -Description: 'A custom class that wraps around string objects.' +Description: 'A custom wrapper for string objects.' Subjects: - 'Computer Science' Tags: @@ -9,4 +9,8 @@ Tags: CatalogContent: - 'learn-python-3' - 'paths/computer-science' ---- \ No newline at end of file +--- + +A **UserString* is a class in the `collections` module. It is a custom wrapper for string objects, behaving like a string but allowing easier subclassing. Unlike directly subclassing [`str`]((https://www.codecademy.com/resources/docs/python/dictionaries)), `UserString` stores its content in the `.data` attribute. The seq parameter passed at initialization can be anything that can be converted into a `str`. + +**Note:** A `UserString` is a wrapper class that behaves like a sequence. The actual string is stored in the `.data` attribute. \ No newline at end of file From b03cbe8fc52cab4c3538c187c0a3270c7d54af93 Mon Sep 17 00:00:00 2001 From: Alessandro Capialbi Date: Thu, 4 Sep 2025 20:17:56 +0200 Subject: [PATCH 03/11] docs: Syntax and Codebyte example --- .../terms/userstring/userstring.md | 34 ++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/content/python/concepts/collections-module/terms/userstring/userstring.md b/content/python/concepts/collections-module/terms/userstring/userstring.md index dd2691c7e99..1041e0ddc2a 100644 --- a/content/python/concepts/collections-module/terms/userstring/userstring.md +++ b/content/python/concepts/collections-module/terms/userstring/userstring.md @@ -13,4 +13,36 @@ CatalogContent: A **UserString* is a class in the `collections` module. It is a custom wrapper for string objects, behaving like a string but allowing easier subclassing. Unlike directly subclassing [`str`]((https://www.codecademy.com/resources/docs/python/dictionaries)), `UserString` stores its content in the `.data` attribute. The seq parameter passed at initialization can be anything that can be converted into a `str`. -**Note:** A `UserString` is a wrapper class that behaves like a sequence. The actual string is stored in the `.data` attribute. \ No newline at end of file +**Note:** A `UserString` is a wrapper class that behaves like a sequence. The actual string is stored in the `.data` attribute. + +## Syntax + +```pseudo +myString = collections.UserString(seq) +``` + +A `UserString` can be initialized either as a sequence of chars or a string. It doesn't provide any additional method. + +## Codebyte Example + +The following example creates a `UserString` from an iterable (a string) and outputs various characteristics of the `UserString`: + +```codebyte/python +from collections import UserString +myString = 'Welcome to Codecademy Docs!' +customString = UserString(myString) +print(f"My personal UserString: {customString.data}\n") +print(f"{customString.upper()}\n") +print(f"Substring of the first word: {customString[0 : 7]}\n") + +class NoVowels(UserString): + def __init__(self, seq): + # Remove vowels from the string + super().__init__(''.join(ch for ch in seq if ch.lower() not in "aeiou")) + +s = NoVowels("Hello World") +print(f"Word without any vowels left: {s}") +``` + + + From 0128c18304c6e2bc829157eb64ae1f958466e9a8 Mon Sep 17 00:00:00 2001 From: Alessandro Capialbi Date: Thu, 4 Sep 2025 20:18:32 +0200 Subject: [PATCH 04/11] fix: indentation --- .../concepts/collections-module/terms/userstring/userstring.md | 3 --- 1 file changed, 3 deletions(-) diff --git a/content/python/concepts/collections-module/terms/userstring/userstring.md b/content/python/concepts/collections-module/terms/userstring/userstring.md index 1041e0ddc2a..2de44bf607b 100644 --- a/content/python/concepts/collections-module/terms/userstring/userstring.md +++ b/content/python/concepts/collections-module/terms/userstring/userstring.md @@ -43,6 +43,3 @@ class NoVowels(UserString): s = NoVowels("Hello World") print(f"Word without any vowels left: {s}") ``` - - - From c726be2bdd402cc454c03aedb13430c0f8c99c93 Mon Sep 17 00:00:00 2001 From: Alessandro Capialbi Date: Thu, 4 Sep 2025 21:15:37 +0200 Subject: [PATCH 05/11] fix: format:verify --- .../collections-module/terms/userstring/userstring.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/content/python/concepts/collections-module/terms/userstring/userstring.md b/content/python/concepts/collections-module/terms/userstring/userstring.md index 2de44bf607b..89f6762e2d1 100644 --- a/content/python/concepts/collections-module/terms/userstring/userstring.md +++ b/content/python/concepts/collections-module/terms/userstring/userstring.md @@ -1,17 +1,17 @@ --- Title: 'collections.UserString' Description: 'A custom wrapper for string objects.' -Subjects: +Subjects: - 'Computer Science' -Tags: +Tags: - 'Strings' - 'Data Types' -CatalogContent: +CatalogContent: - 'learn-python-3' - 'paths/computer-science' --- -A **UserString* is a class in the `collections` module. It is a custom wrapper for string objects, behaving like a string but allowing easier subclassing. Unlike directly subclassing [`str`]((https://www.codecademy.com/resources/docs/python/dictionaries)), `UserString` stores its content in the `.data` attribute. The seq parameter passed at initialization can be anything that can be converted into a `str`. +A \*_UserString_ is a class in the `collections` module. It is a custom wrapper for string objects, behaving like a string but allowing easier subclassing. Unlike directly subclassing [`str`](<(https://www.codecademy.com/resources/docs/python/dictionaries)>), `UserString` stores its content in the `.data` attribute. The seq parameter passed at initialization can be anything that can be converted into a `str`. **Note:** A `UserString` is a wrapper class that behaves like a sequence. The actual string is stored in the `.data` attribute. @@ -41,5 +41,5 @@ class NoVowels(UserString): super().__init__(''.join(ch for ch in seq if ch.lower() not in "aeiou")) s = NoVowels("Hello World") -print(f"Word without any vowels left: {s}") +print(f"Word without any vowels left: {s}") ``` From d4706095ccfccee4b5ad29ed8af3bec3e9b613b2 Mon Sep 17 00:00:00 2001 From: Alessandro Capialbi Date: Thu, 4 Sep 2025 21:18:28 +0200 Subject: [PATCH 06/11] fix: format:verify --- .../concepts/collections-module/terms/userstring/userstring.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/python/concepts/collections-module/terms/userstring/userstring.md b/content/python/concepts/collections-module/terms/userstring/userstring.md index 89f6762e2d1..27d0cd99e6a 100644 --- a/content/python/concepts/collections-module/terms/userstring/userstring.md +++ b/content/python/concepts/collections-module/terms/userstring/userstring.md @@ -11,7 +11,7 @@ CatalogContent: - 'paths/computer-science' --- -A \*_UserString_ is a class in the `collections` module. It is a custom wrapper for string objects, behaving like a string but allowing easier subclassing. Unlike directly subclassing [`str`](<(https://www.codecademy.com/resources/docs/python/dictionaries)>), `UserString` stores its content in the `.data` attribute. The seq parameter passed at initialization can be anything that can be converted into a `str`. +A `UserString` is a class in the `collections` module. It is a custom wrapper for string objects, behaving like a string but allowing easier subclassing. Unlike directly subclassing [`str`](<(https://www.codecademy.com/resources/docs/python/dictionaries)>), `UserString` stores its content in the `.data` attribute. The seq parameter passed at initialization can be anything that can be converted into a `str`. **Note:** A `UserString` is a wrapper class that behaves like a sequence. The actual string is stored in the `.data` attribute. From c0fd3fc8dd295de78cb66650cbff1e4bdebee018 Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Mon, 8 Sep 2025 17:46:29 +0530 Subject: [PATCH 07/11] minor wording fixes --- .../terms/userstring/userstring.md | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/content/python/concepts/collections-module/terms/userstring/userstring.md b/content/python/concepts/collections-module/terms/userstring/userstring.md index 27d0cd99e6a..01d6af7a6ff 100644 --- a/content/python/concepts/collections-module/terms/userstring/userstring.md +++ b/content/python/concepts/collections-module/terms/userstring/userstring.md @@ -1,19 +1,20 @@ --- -Title: 'collections.UserString' -Description: 'A custom wrapper for string objects.' +Title: 'UserString' +Description: 'A wrapper class for string objects that makes subclassing easier.' Subjects: - 'Computer Science' + - 'Data Science' Tags: - - 'Strings' - 'Data Types' + - 'Strings' CatalogContent: - 'learn-python-3' - 'paths/computer-science' --- -A `UserString` is a class in the `collections` module. It is a custom wrapper for string objects, behaving like a string but allowing easier subclassing. Unlike directly subclassing [`str`](<(https://www.codecademy.com/resources/docs/python/dictionaries)>), `UserString` stores its content in the `.data` attribute. The seq parameter passed at initialization can be anything that can be converted into a `str`. +A **`UserString`** is a class in the `collections` module. It is a custom wrapper for string objects, behaving like a string but allowing easier subclassing. Unlike directly subclassing `str`, `UserString` stores its content in the `.data` attribute. The `seq` parameter passed at initialization can be any object that can be converted into a string. -**Note:** A `UserString` is a wrapper class that behaves like a sequence. The actual string is stored in the `.data` attribute. +> **Note:** While `UserString` behaves like a string and supports the same operations, some methods return a regular `str` instead of another `UserString`. ## Syntax @@ -25,7 +26,7 @@ A `UserString` can be initialized either as a sequence of chars or a string. It ## Codebyte Example -The following example creates a `UserString` from an iterable (a string) and outputs various characteristics of the `UserString`: +The following example creates a `UserString` and demonstrates its usage, along with a subclass that removes vowels: ```codebyte/python from collections import UserString @@ -36,9 +37,9 @@ print(f"{customString.upper()}\n") print(f"Substring of the first word: {customString[0 : 7]}\n") class NoVowels(UserString): - def __init__(self, seq): - # Remove vowels from the string - super().__init__(''.join(ch for ch in seq if ch.lower() not in "aeiou")) + def __init__(self, seq): + # Remove vowels from the string + super().__init__(''.join(ch for ch in seq if ch.lower() not in "aeiou")) s = NoVowels("Hello World") print(f"Word without any vowels left: {s}") From f03a0c651b3c45d93389e9cbf41341c40bc73406 Mon Sep 17 00:00:00 2001 From: Alessandro Capialbi Date: Tue, 9 Sep 2025 22:01:21 +0200 Subject: [PATCH 08/11] docs: example and list of parameters --- .../terms/userstring/userstring.md | 28 +++++++++++++++++-- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/content/python/concepts/collections-module/terms/userstring/userstring.md b/content/python/concepts/collections-module/terms/userstring/userstring.md index 27d0cd99e6a..f9a539e887f 100644 --- a/content/python/concepts/collections-module/terms/userstring/userstring.md +++ b/content/python/concepts/collections-module/terms/userstring/userstring.md @@ -11,7 +11,7 @@ CatalogContent: - 'paths/computer-science' --- -A `UserString` is a class in the `collections` module. It is a custom wrapper for string objects, behaving like a string but allowing easier subclassing. Unlike directly subclassing [`str`](<(https://www.codecademy.com/resources/docs/python/dictionaries)>), `UserString` stores its content in the `.data` attribute. The seq parameter passed at initialization can be anything that can be converted into a `str`. +A `UserString` is a class in the `collections` module. It is a custom wrapper for string objects, behaving like a string but allowing easier subclassing. Unlike directly subclassing [`str`](<(https://www.codecademy.com/resources/docs/python/dictionaries)>), `UserString` stores its content in the `.data` attribute. **Note:** A `UserString` is a wrapper class that behaves like a sequence. The actual string is stored in the `.data` attribute. @@ -21,11 +21,33 @@ A `UserString` is a class in the `collections` module. It is a custom wrapper fo myString = collections.UserString(seq) ``` -A `UserString` can be initialized either as a sequence of chars or a string. It doesn't provide any additional method. +- `seq`: It can be anything that can be converted into a `str` or `iterable of characters`, like a list or tuple of individual characters. + +Return value: + +- A `UserString` instance, which is an object that contains the content derived from `seq`. + +## Example + +The following example demonstrates the usage of the `UserString` method: + +```py +from collections import UserString + +myString = 'First example of a UserString!' +customString = UserString(myString) +print(customString.data) +``` + +This is the output of the declared code: + +```shell +'First example of a UserString!' +``` ## Codebyte Example -The following example creates a `UserString` from an iterable (a string) and outputs various characteristics of the `UserString`: +The following example creates a `UserString` from an iterable (a string) and outputs more details about the characteristics of the `UserString`: ```codebyte/python from collections import UserString From 7611c2ef4c68b7563b4e57288b8964cc6333fb47 Mon Sep 17 00:00:00 2001 From: Alessandro Capialbi Date: Tue, 9 Sep 2025 22:04:57 +0200 Subject: [PATCH 09/11] fix: minor wording fixes --- .../terms/userstring/userstring.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/content/python/concepts/collections-module/terms/userstring/userstring.md b/content/python/concepts/collections-module/terms/userstring/userstring.md index f9a539e887f..cd9b863532c 100644 --- a/content/python/concepts/collections-module/terms/userstring/userstring.md +++ b/content/python/concepts/collections-module/terms/userstring/userstring.md @@ -1,6 +1,6 @@ --- Title: 'collections.UserString' -Description: 'A custom wrapper for string objects.' +Description: 'A wrapper class for string objects that makes subclassing easier.' Subjects: - 'Computer Science' Tags: @@ -13,7 +13,7 @@ CatalogContent: A `UserString` is a class in the `collections` module. It is a custom wrapper for string objects, behaving like a string but allowing easier subclassing. Unlike directly subclassing [`str`](<(https://www.codecademy.com/resources/docs/python/dictionaries)>), `UserString` stores its content in the `.data` attribute. -**Note:** A `UserString` is a wrapper class that behaves like a sequence. The actual string is stored in the `.data` attribute. +**Note:** While `UserString` behaves like a string and supports the same operations, some methods return a regular `str` instead of another `UserString`. ## Syntax @@ -47,7 +47,7 @@ This is the output of the declared code: ## Codebyte Example -The following example creates a `UserString` from an iterable (a string) and outputs more details about the characteristics of the `UserString`: +The following example creates a `UserString` and demonstrates its usage more in detail, along with a subclass that removes vowels: ```codebyte/python from collections import UserString @@ -58,9 +58,9 @@ print(f"{customString.upper()}\n") print(f"Substring of the first word: {customString[0 : 7]}\n") class NoVowels(UserString): - def __init__(self, seq): - # Remove vowels from the string - super().__init__(''.join(ch for ch in seq if ch.lower() not in "aeiou")) + def __init__(self, seq): + # Remove vowels from the string + super().__init__(''.join(ch for ch in seq if ch.lower() not in "aeiou")) s = NoVowels("Hello World") print(f"Word without any vowels left: {s}") From f6c1ab64d996f691c404449b1304d80ac67791a6 Mon Sep 17 00:00:00 2001 From: Alessandro Capialbi Date: Tue, 9 Sep 2025 22:19:44 +0200 Subject: [PATCH 10/11] fix: minor wording fixes --- .../concepts/collections-module/terms/userstring/userstring.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/python/concepts/collections-module/terms/userstring/userstring.md b/content/python/concepts/collections-module/terms/userstring/userstring.md index cd9b863532c..331e302009c 100644 --- a/content/python/concepts/collections-module/terms/userstring/userstring.md +++ b/content/python/concepts/collections-module/terms/userstring/userstring.md @@ -39,7 +39,7 @@ customString = UserString(myString) print(customString.data) ``` -This is the output of the declared code: +This is the output of the above code: ```shell 'First example of a UserString!' From d23bca905f592081dcb4d7183bb46bda7b5e9208 Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Tue, 16 Sep 2025 10:43:54 +0530 Subject: [PATCH 11/11] minor fixes --- .../terms/userstring/userstring.md | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/content/python/concepts/collections-module/terms/userstring/userstring.md b/content/python/concepts/collections-module/terms/userstring/userstring.md index 331e302009c..80012a77b63 100644 --- a/content/python/concepts/collections-module/terms/userstring/userstring.md +++ b/content/python/concepts/collections-module/terms/userstring/userstring.md @@ -1,19 +1,20 @@ --- -Title: 'collections.UserString' +Title: 'UserString' Description: 'A wrapper class for string objects that makes subclassing easier.' Subjects: - 'Computer Science' + - 'Data Science' Tags: - - 'Strings' - 'Data Types' + - 'Strings' CatalogContent: - 'learn-python-3' - 'paths/computer-science' --- -A `UserString` is a class in the `collections` module. It is a custom wrapper for string objects, behaving like a string but allowing easier subclassing. Unlike directly subclassing [`str`](<(https://www.codecademy.com/resources/docs/python/dictionaries)>), `UserString` stores its content in the `.data` attribute. +A **`UserString`** is a class in the `collections` module. It is a custom wrapper for string objects, behaving like a string but allowing easier subclassing. Unlike directly subclassing `str`, `UserString` stores its content in the `.data` attribute. -**Note:** While `UserString` behaves like a string and supports the same operations, some methods return a regular `str` instead of another `UserString`. +> **Note:** While `UserString` behaves like a string and supports the same operations, some methods return a regular `str` instead of another `UserString`. ## Syntax @@ -21,7 +22,7 @@ A `UserString` is a class in the `collections` module. It is a custom wrapper fo myString = collections.UserString(seq) ``` -- `seq`: It can be anything that can be converted into a `str` or `iterable of characters`, like a list or tuple of individual characters. +- `seq`: It can be anything that can be converted into a `str` or `iterable of characters`, like a [list](https://www.codecademy.com/resources/docs/python/lists) or [tuple](https://www.codecademy.com/resources/docs/python/tuples) of individual characters. Return value: @@ -42,7 +43,7 @@ print(customString.data) This is the output of the above code: ```shell -'First example of a UserString!' +First example of a UserString! ``` ## Codebyte Example @@ -58,9 +59,9 @@ print(f"{customString.upper()}\n") print(f"Substring of the first word: {customString[0 : 7]}\n") class NoVowels(UserString): - def __init__(self, seq): - # Remove vowels from the string - super().__init__(''.join(ch for ch in seq if ch.lower() not in "aeiou")) + def __init__(self, seq): + # Remove vowels from the string + super().__init__(''.join(ch for ch in seq if ch.lower() not in "aeiou")) s = NoVowels("Hello World") print(f"Word without any vowels left: {s}")