-
Notifications
You must be signed in to change notification settings - Fork 4.1k
[Edit] Python: .lower() #7028
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
Merged
Merged
[Edit] Python: .lower() #7028
Changes from 17 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
d0dd9bd
[Edit] SQL: DATEDIFF()
mamtawardhani 9f5c19b
Update datediff.md
mamtawardhani c32e9f3
Merge branch 'Codecademy:main' into main
mamtawardhani 4170ba2
Merge branch 'Codecademy:main' into main
mamtawardhani 8325585
Merge branch 'Codecademy:main' into main
mamtawardhani 8f6f8e8
Merge branch 'Codecademy:main' into main
mamtawardhani e4c54e8
Merge branch 'Codecademy:main' into main
mamtawardhani 7b3b9c0
Merge branch 'Codecademy:main' into main
mamtawardhani 27ecefd
Merge branch 'Codecademy:main' into main
mamtawardhani 0392da4
Merge branch 'Codecademy:main' into main
mamtawardhani d550fa7
Merge branch 'Codecademy:main' into main
mamtawardhani 793be7d
Merge branch 'Codecademy:main' into main
mamtawardhani 2f03b61
Merge branch 'Codecademy:main' into main
mamtawardhani 25eb0ab
Merge branch 'Codecademy:main' into main
mamtawardhani 73e0e3b
Merge branch 'Codecademy:main' into main
mamtawardhani 44f4c63
Merge branch 'Codecademy:main' into main
mamtawardhani 429433e
[Edit] Python: .lower()
mamtawardhani d42081b
Update content/python/concepts/strings/terms/lower/lower.md
avdhoottt 7e615a0
Update content/python/concepts/strings/terms/lower/lower.md
avdhoottt 8ac5721
Update content/python/concepts/strings/terms/lower/lower.md
avdhoottt 2046fde
Merge branch 'main' into py-strings-lower
avdhoottt 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
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 |
---|---|---|
@@ -1,83 +1,134 @@ | ||
--- | ||
Title: .lower() | ||
Description: 'Takes a string, and returns a copy of that string in which all letters are lowercase. Numbers and symbols are not changed.' | ||
Title: '.lower()' | ||
Description: 'Converts all uppercase characters in a string to lowercase and returns a new string.' | ||
Subjects: | ||
- 'Data Science' | ||
- 'Computer Science' | ||
- 'Data Science' | ||
Tags: | ||
- 'Strings' | ||
- 'Methods' | ||
- 'Functions' | ||
- 'Methods' | ||
- 'Strings' | ||
CatalogContent: | ||
- 'learn-python-3' | ||
- 'paths/analyze-data-with-python' | ||
- 'paths/computer-science' | ||
--- | ||
|
||
Takes a string, and returns a copy of that string in which all letters are lowercase. Numbers and symbols are not changed. | ||
The **`.lower()`** method is a built-in string method in Python that converts all uppercase characters in a string to lowercase. This method does not modify the original string; instead, it returns a new string with all alphabetic characters converted to their lowercase equivalents. Non-alphabetic characters such as numbers, punctuation marks, and special symbols remain unchanged. | ||
|
||
The `.lower()` method is commonly used in scenarios where case-insensitive operations are required, such as user input validation, data normalization, string comparisons, search functionality, and email address standardization. It is especially useful in text processing tasks that require consistent formatting for reliable data handling and user interaction. | ||
|
||
## Syntax | ||
|
||
```pseudo | ||
string.lower() | ||
``` | ||
|
||
## Example 1 | ||
**Parameters:** | ||
|
||
The `.lower()` method can be used to compare strings: | ||
- This method does not take any parameters. | ||
|
||
```python | ||
string1 = "Red Pandas" | ||
string2 = "rEd pAnDaS" | ||
**Return value:** | ||
|
||
if string1 == string2: | ||
print("These strings are already the same") | ||
elif string1.lower() == string2.lower(): | ||
print("They are the same when you use the .lower() method") | ||
else: | ||
print("They are NOT the same") | ||
The `.lower()` method returns a new string with all uppercase characters converted to lowercase. The original string remains unchanged since strings in Python are immutable. | ||
|
||
# Output: They are the same when you use .lower() | ||
## Example 1: Basic String Conversion | ||
|
||
This example demonstrates the fundamental usage of the `.lower()` method with a simple string conversion: | ||
avdhoottt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
```py | ||
# Original string with mixed case | ||
message = "Hello World! Welcome to PYTHON Programming." | ||
|
||
# Convert to lowercase | ||
lowercase_message = message.lower() | ||
print(f"Original: {message}") | ||
print(f"Lowercase: {lowercase_message}") | ||
``` | ||
|
||
## Example 2 | ||
The output produced by this code is: | ||
|
||
```shell | ||
Original: Hello World! Welcome to PYTHON Programming. | ||
Lowercase: hello world! welcome to python programming. | ||
``` | ||
|
||
This example shows how `.lower()` converts all uppercase and mixed-case letters to lowercase while preserving punctuation and spacing. The method creates a new string object, leaving the original string unchanged. | ||
|
||
## Example 2: Case-Insensitive User Authentication | ||
|
||
The `.lower()` method can be used to standardize text that might take different forms, such as user input or the response to an API call: | ||
This example demonstrates using `.lower()` for case-insensitive username validation in a login system: | ||
avdhoottt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
```python | ||
name = input("What is your name?") | ||
# User writes their name... | ||
```py | ||
# Simulate a user database with lowercase usernames | ||
registered_users = ["john_doe", "alice_smith", "bob_wilson"] | ||
|
||
if name.lower() == "codey": | ||
print("Your name is Codey!") | ||
# Get user input | ||
user_input = input("Enter your username: ") # User might enter "JOHN_DOE" | ||
|
||
# Normalize input for case-insensitive comparison | ||
normalized_input = user_input.lower() | ||
|
||
# Check if user exists | ||
if normalized_input in registered_users: | ||
print(f"Welcome back, {user_input}!") | ||
else: | ||
print("Your name is not Codey.") | ||
print("Username not found. Please check and try again.") | ||
``` | ||
|
||
This would print `Your name is Codey!` whether the user typed in `Codey`, `codey`, `CODEY`, or `CoDeY`. | ||
The output of this code will be: | ||
|
||
## Example 3 | ||
```shell | ||
Enter your username: JOHN_DOE | ||
Welcome back, JOHN_DOE! | ||
``` | ||
|
||
The `.lower()` method does not change the string it is used on: | ||
This example illustrates how `.lower()` enables case-insensitive authentication by normalizing user input before comparing it against stored usernames. This approach ensures that users can log in regardless of how they type their username. | ||
|
||
```python | ||
my_string = "AMAZING!" | ||
## Codebyte Example: Email Address Standardization | ||
|
||
if my_string.lower() == "amazing!": | ||
print("Isn't that just " + my_string) | ||
This example shows how `.lower()` is used to standardize email addresses for consistent data storage and comparison: | ||
|
||
# Output: "Isn't that just AMAZING!"" | ||
```codebyte/python | ||
# List of user email addresses with inconsistent casing | ||
email_list = [ | ||
"John.DOE@Gmail.COM", | ||
"alice.SMITH@yahoo.com", | ||
"Bob.Wilson@HOTMAIL.com", | ||
"sarah.jones@COMPANY.org" | ||
] | ||
|
||
# Standardize all email addresses to lowercase | ||
standardized_emails = [] | ||
for email in email_list: | ||
standardized_email = email.lower() | ||
standardized_emails.append(standardized_email) | ||
print(f"Original: {email} → Standardized: {standardized_email}") | ||
|
||
# Check for duplicate emails after standardization | ||
print(f"\nTotal emails: {len(standardized_emails)}") | ||
print(f"Unique emails: {len(set(standardized_emails))}") | ||
``` | ||
|
||
## Codebyte Example | ||
This example demonstrates how `.lower()` helps maintain data consistency by standardizing email addresses to lowercase format. This is essential for preventing duplicate accounts and ensuring reliable email-based operations in applications. | ||
|
||
The example below compares `color_entry_a` to `color_entry_b` using `.lower()`. Note `color_entry_a` remains capitalized even after the `.lower()` method is used on it. | ||
## Frequently Asked Questions | ||
|
||
```codebyte/python | ||
color_entry_a = "Blue" | ||
color_entry_b = "blue" | ||
### 1. Does `.lower()` modify the original string? | ||
|
||
if color_entry_a.lower() == color_entry_b.lower(): | ||
print("We both like the color " + color_entry_a + "!") | ||
else: | ||
print("We like different colors.") | ||
``` | ||
No, `.lower()` does not modify the original string. It returns a new string with the converted characters since strings in Python are immutable. | ||
|
||
### 2. What happens to numbers and special characters? | ||
|
||
Numbers, punctuation marks, and special characters remain unchanged when using `.lower()`. Only alphabetic characters are converted to lowercase. | ||
|
||
### 3. Can I use `.lower()` with non-English characters? | ||
|
||
Yes, `.lower()` works with Unicode characters and supports international alphabets. For example, `"CAFÉ".lower()` returns `"café"`. | ||
|
||
### 4. How does `.lower()` handle empty strings? | ||
|
||
When applied to an empty string, `.lower()` returns an empty string: `"".lower()` returns `""`. | ||
|
||
### 5. What's the difference between `.lower()` and `.casefold()`? | ||
|
||
While both convert strings to lowercase, `.casefold()` is more aggressive and better suited for case-insensitive comparisons involving special Unicode characters. For most English text applications, `.lower()` is sufficient and more commonly used. |
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.