-
Notifications
You must be signed in to change notification settings - Fork 4.2k
[Term Entry] Userstring Python collection-module #7569
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
alessandrocapialbi
wants to merge
18
commits into
Codecademy:main
Choose a base branch
from
alessandrocapialbi:userstring-entry
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
aaf95ef
docs: init metadata
alessandrocapialbi 515ad01
docs: brief description
alessandrocapialbi b03cbe8
docs: Syntax and Codebyte example
alessandrocapialbi 0128c18
fix: indentation
alessandrocapialbi c726be2
fix: format:verify
alessandrocapialbi d470609
fix: format:verify
alessandrocapialbi 8f83e76
Merge branch 'main' into userstring-entry
alessandrocapialbi e689dcf
Merge branch 'main' into userstring-entry
mamtawardhani c0fd3fc
minor wording fixes
mamtawardhani f03a0c6
docs: example and list of parameters
alessandrocapialbi 7611c2e
fix: minor wording fixes
alessandrocapialbi ec32c34
B
alessandrocapialbi f6c1ab6
fix: minor wording fixes
alessandrocapialbi 518740c
Merge branch 'main' into userstring-entry
alessandrocapialbi d23bca9
minor fixes
mamtawardhani 52fdc0e
Merge branch 'main' into userstring-entry
mamtawardhani 7588613
Merge branch 'main' into userstring-entry
alessandrocapialbi 61aa55d
Merge branch 'main' into userstring-entry
alessandrocapialbi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
68 changes: 68 additions & 0 deletions
68
content/python/concepts/collections-module/terms/userstring/userstring.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
--- | ||
Title: 'UserString' | ||
Description: 'A wrapper class for string objects that makes subclassing easier.' | ||
Subjects: | ||
- 'Computer Science' | ||
- 'Data Science' | ||
Tags: | ||
- '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`, `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`. | ||
|
||
## Syntax | ||
|
||
```pseudo | ||
myString = collections.UserString(seq) | ||
``` | ||
|
||
- `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: | ||
|
||
- 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 above code: | ||
|
||
```shell | ||
First example of a UserString! | ||
``` | ||
|
||
## Codebyte Example | ||
|
||
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 | ||
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}") | ||
``` |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.