Skip to content

Commit b6df18c

Browse files
[Edit] Python: .lower() (#7028)
* [Edit] SQL: DATEDIFF() * Update datediff.md * [Edit] Python: .lower() * Update content/python/concepts/strings/terms/lower/lower.md * Update content/python/concepts/strings/terms/lower/lower.md * Update content/python/concepts/strings/terms/lower/lower.md ---------
1 parent 23d5b24 commit b6df18c

File tree

1 file changed

+96
-45
lines changed
  • content/python/concepts/strings/terms/lower

1 file changed

+96
-45
lines changed
Lines changed: 96 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,134 @@
11
---
2-
Title: .lower()
3-
Description: 'Takes a string, and returns a copy of that string in which all letters are lowercase. Numbers and symbols are not changed.'
2+
Title: '.lower()'
3+
Description: 'Converts all uppercase characters in a string to lowercase and returns a new string.'
44
Subjects:
5-
- 'Data Science'
65
- 'Computer Science'
6+
- 'Data Science'
77
Tags:
8-
- 'Strings'
9-
- 'Methods'
108
- 'Functions'
9+
- 'Methods'
10+
- 'Strings'
1111
CatalogContent:
1212
- 'learn-python-3'
13-
- 'paths/analyze-data-with-python'
13+
- 'paths/computer-science'
1414
---
1515

16-
Takes a string, and returns a copy of that string in which all letters are lowercase. Numbers and symbols are not changed.
16+
The **`.lower()`** method is a built-in [string](https://www.codecademy.com/resources/docs/python/strings) 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.
17+
18+
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.
1719

1820
## Syntax
1921

2022
```pseudo
2123
string.lower()
2224
```
2325

24-
## Example 1
26+
**Parameters:**
2527

26-
The `.lower()` method can be used to compare strings:
28+
- This method does not take any parameters.
2729

28-
```python
29-
string1 = "Red Pandas"
30-
string2 = "rEd pAnDaS"
30+
**Return value:**
3131

32-
if string1 == string2:
33-
print("These strings are already the same")
34-
elif string1.lower() == string2.lower():
35-
print("They are the same when you use the .lower() method")
36-
else:
37-
print("They are NOT the same")
32+
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.
3833

39-
# Output: They are the same when you use .lower()
34+
## Example 1: Basic String Conversion
35+
36+
The following example demonstrates the fundamental usage of the `.lower()` method with a simple string conversion:
37+
38+
```py
39+
# Original string with mixed case
40+
message = "Hello World! Welcome to PYTHON Programming."
41+
42+
# Convert to lowercase
43+
lowercase_message = message.lower()
44+
print(f"Original: {message}")
45+
print(f"Lowercase: {lowercase_message}")
4046
```
4147

42-
## Example 2
48+
The output produced by this code is:
49+
50+
```shell
51+
Original: Hello World! Welcome to PYTHON Programming.
52+
Lowercase: hello world! welcome to python programming.
53+
```
54+
55+
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.
56+
57+
## Example 2: Case-Insensitive User Authentication
4358

44-
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:
59+
The following example demonstrates using `.lower()` for case-insensitive username validation in a login system:
4560

46-
```python
47-
name = input("What is your name?")
48-
# User writes their name...
61+
```py
62+
# Simulate a user database with lowercase usernames
63+
registered_users = ["john_doe", "alice_smith", "bob_wilson"]
4964

50-
if name.lower() == "codey":
51-
print("Your name is Codey!")
65+
# Get user input
66+
user_input = input("Enter your username: ") # User might enter "JOHN_DOE"
67+
68+
# Normalize input for case-insensitive comparison
69+
normalized_input = user_input.lower()
70+
71+
# Check if user exists
72+
if normalized_input in registered_users:
73+
print(f"Welcome back, {user_input}!")
5274
else:
53-
print("Your name is not Codey.")
75+
print("Username not found. Please check and try again.")
5476
```
5577

56-
This would print `Your name is Codey!` whether the user typed in `Codey`, `codey`, `CODEY`, or `CoDeY`.
78+
The output of this code will be:
5779

58-
## Example 3
80+
```shell
81+
Enter your username: JOHN_DOE
82+
Welcome back, JOHN_DOE!
83+
```
5984

60-
The `.lower()` method does not change the string it is used on:
85+
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.
6186

62-
```python
63-
my_string = "AMAZING!"
87+
## Codebyte Example: Email Address Standardization
6488

65-
if my_string.lower() == "amazing!":
66-
print("Isn't that just " + my_string)
89+
This example shows how `.lower()` is used to standardize email addresses for consistent data storage and comparison:
6790

68-
# Output: "Isn't that just AMAZING!""
91+
```codebyte/python
92+
# List of user email addresses with inconsistent casing
93+
email_list = [
94+
95+
96+
97+
98+
]
99+
100+
# Standardize all email addresses to lowercase
101+
standardized_emails = []
102+
for email in email_list:
103+
standardized_email = email.lower()
104+
standardized_emails.append(standardized_email)
105+
print(f"Original: {email} → Standardized: {standardized_email}")
106+
107+
# Check for duplicate emails after standardization
108+
print(f"\nTotal emails: {len(standardized_emails)}")
109+
print(f"Unique emails: {len(set(standardized_emails))}")
69110
```
70111

71-
## Codebyte Example
112+
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.
72113

73-
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.
114+
## Frequently Asked Questions
74115

75-
```codebyte/python
76-
color_entry_a = "Blue"
77-
color_entry_b = "blue"
116+
### 1. Does `.lower()` modify the original string?
78117

79-
if color_entry_a.lower() == color_entry_b.lower():
80-
print("We both like the color " + color_entry_a + "!")
81-
else:
82-
print("We like different colors.")
83-
```
118+
No, `.lower()` does not modify the original string. It returns a new string with the converted characters since strings in Python are immutable.
119+
120+
### 2. What happens to numbers and special characters?
121+
122+
Numbers, punctuation marks, and special characters remain unchanged when using `.lower()`. Only alphabetic characters are converted to lowercase.
123+
124+
### 3. Can I use `.lower()` with non-English characters?
125+
126+
Yes, `.lower()` works with Unicode characters and supports international alphabets. For example, `"CAFÉ".lower()` returns `"café"`.
127+
128+
### 4. How does `.lower()` handle empty strings?
129+
130+
When applied to an empty string, `.lower()` returns an empty string: `"".lower()` returns `""`.
131+
132+
### 5. What's the difference between `.lower()` and `.casefold()`?
133+
134+
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.

0 commit comments

Comments
 (0)