-
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 8 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
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
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,45 @@ | ||
--- | ||
Title: 'collections.UserString' | ||
Description: 'A custom wrapper for string objects.' | ||
Subjects: | ||
- 'Computer Science' | ||
Tags: | ||
- 'Strings' | ||
- 'Data Types' | ||
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`. | ||
|
||
**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 | ||
alessandrocapialbi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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}") | ||
``` |
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.