Skip to content

Commit 8f3ac39

Browse files
committed
add more examples in working-with-text.md (length, upper/lower)
1 parent 35ed3ca commit 8f3ac39

File tree

1 file changed

+77
-3
lines changed

1 file changed

+77
-3
lines changed

src/concepts/module-2/working-with-text.md

Lines changed: 77 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
# Chopping and Combining (Working with Text)
22

3-
Working with numbers is about calculation, but working with text—or **strings**—is about communication. The most common task you'll perform with strings is combining them to create new, more meaningful messages.
3+
Working with numbers is about calculation, but working with text—or **strings**—is about communication. You'll constantly need to prepare your text to be shown to a user, whether it's a welcome message, a menu item, or an error warning.
44

5-
This process of joining strings together is called **concatenation**. 🔗
5+
Let's look at the three most common ways you'll work with strings: combining them, inspecting their properties, and changing their style.
6+
7+
### 1. Combining: "Pen Pineapple Apple Pen"
8+
9+
The most frequent task you'll perform with strings is joining them together to create new, more meaningful messages. This process is called **concatenation**. 🔗
610

711
It's like connecting two train cars to make a longer train. In many languages, you can use the same `+` symbol you used for addition to concatenate strings.
812

@@ -34,4 +38,74 @@ You got [Pen Pineapple Apple Pen](https://www.youtube.com/watch?v=NfuiB52K7X8)!
3438

3539
Notice that we had to add a space `" "` in the middle. The computer is extremely literal; it only combines *exactly* what you give it. Without that space, the result would have been `"Pen PineappleApple Pen"`.
3640

37-
This is a fundamental building block. You'll use it to create dynamic text (`"Order: " + orderItem`), generate reports, or display any kind of organized text to a user.
41+
### 2. Inspecting: "Is the Username Too Long?"
42+
43+
Sometimes you don't need to change the text, but you need to get information *about* it. The most common piece of information you'll need is its **length**.
44+
45+
This is useful for checking things like, "Is this username less than 15 characters?" or "Will this menu item name fit on the display?"
46+
47+
<!-- langtabs-start -->
48+
49+
```py
50+
# In Python, we use the len() function to get the length of a string.
51+
menu_item = "Extra Cheesy Supreme Pizza"
52+
item_length = len(menu_item)
53+
54+
print("Menu Item:", menu_item)
55+
print("Character Count:", item_length)
56+
57+
# Now we can use this information in a decision!
58+
if item_length > 20:
59+
print("Warning: This name might be too long for the menu board!")
60+
61+
```
62+
63+
```js
64+
// In JavaScript, we use the .length property to get the length of a string.
65+
let menuItem = "Extra Cheesy Supreme Pizza";
66+
let itemLength = menuItem.length;
67+
68+
console.log("Menu Item:", menuItem);
69+
console.log("Character Count:", itemLength);
70+
71+
// Now we can use this information in a decision!
72+
if (itemLength > 20) {
73+
console.log("Warning: This name might be too long for the menu board!");
74+
}
75+
```
76+
<!-- langtabs-end -->
77+
78+
### 3. Styling: "Shouting the Daily Special"
79+
80+
Often, you'll need to change the case of a string for formatting purposes. For example, you might want to display a heading in all capital letters or normalize user input by converting it all to lowercase.
81+
82+
Think of it like deciding how to write something on a menu board. Do you want to SHOUT IT, or write it in normal case?
83+
84+
<!-- langtabs-start -->
85+
86+
```py
87+
daily_special = "Classic Burger with Fries"
88+
89+
# To make a big headline for the menu board, we use .upper()
90+
shouted_special = daily_special.upper()
91+
print(shouted_special) # Displays: CLASSIC BURGER WITH FRIES
92+
93+
# To store it in a database consistently, we might use .lower()
94+
normalized_special = daily_special.lower()
95+
print(normalized_special) # Displays: classic burger with fries
96+
```
97+
98+
```js
99+
let dailySpecial = "Classic Burger with Fries";
100+
101+
// To make a big headline for the menu board, we use .toUpperCase()
102+
let shoutedSpecial = dailySpecial.toUpperCase();
103+
console.log(shoutedSpecial); // Displays: CLASSIC BURGER WITH FRIES
104+
105+
// To store it in a database consistently, we might use .toLowerCase()
106+
let normalizedSpecial = dailySpecial.toLowerCase();
107+
console.log(normalizedSpecial); // Displays: classic burger with fries
108+
```
109+
<!-- langtabs-end -->
110+
111+
These three operations—combining, checking length, and changing case—are the essential tools in your text-handling toolkit. You'll use them constantly to build dynamic, readable, and user-friendly programs.

0 commit comments

Comments
 (0)