pyodide: loading…

[practice]String & File Ops

String Methods Deep Dive

# theory

essential methods

text = "  Hello, World!  "

text.strip()       # "Hello, World!" - remove whitespace
text.lower()       # "  hello, world!  "
text.upper()       # "  HELLO, WORLD!  "
text.title()       # "  Hello, World!  "

text.replace("World", "Python")  # "  Hello, Python!  "
text.split(",")    # ["  Hello", " World!  "]
"-".join(["a", "b", "c"])  # "a-b-c"

checking content

"hello".startswith("he")   # True
"hello".endswith("lo")     # True
"hello".isalpha()          # True (only letters)
"123".isdigit()            # True (only digits)
"hello123".isalnum()       # True (letters + digits)
"hello" in "hello world"   # True (substring check)

formatting & padding

"42".zfill(5)              # "00042" - pad with zeros
"hi".ljust(10)             # "hi        " - left justify
"hi".rjust(10)             # "        hi" - right justify
"hi".center(10)            # "    hi    " - center

# Format method
"{} is {}".format("Python", "fun")
"{name} is {age}".format(name="Alice", age=25)

slicing

text = "Python"
text[0]      # "P" - first character
text[-1]     # "n" - last character
text[0:3]    # "Pyt" - first 3
text[3:]     # "hon" - from index 3 to end
text[::-1]   # "nohtyP" - reversed

finding & counting

"hello world".find("world")   # 6 (index where found)
"hello world".find("xyz")     # -1 (not found)
"hello world".count("l")      # 3 (occurrences)
"hello world".index("world")  # 6 (like find, but errors if not found)

# examples [3]

# example 01 · cleaning user input

Common pattern for processing user data

1
2
3
4
5
6
7
8
9
10
11
12
13
🐍
Loading PythonSetting up pandas & numpy...
# example 02 · formatting numbers

Pad and format numeric strings

1
2
3
4
5
6
7
8
9
10
11
12
13
🐍
Loading PythonSetting up pandas & numpy...
# example 03 · search and replace

Find and modify text content

1
2
3
4
5
6
7
8
9
10
11
12
13
🐍
Loading PythonSetting up pandas & numpy...

# challenges [2]

# challenge 01/02todo
Given phone = ' (555) 123-4567 ', extract just the digits and print them.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
🐍
Loading PythonSetting up pandas & numpy...
# challenge 02/02todo
Split 'apple,banana,cherry' by comma and join with ' - ' between each. Print the result.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
🐍
Loading PythonSetting up pandas & numpy...

# project

# project-challenge

thread: Sales Performance Dashboard · reward: 50 xp

# brief

The sales data has inconsistent SalesRep name formatting. Some names have extra spaces or inconsistent capitalization. Clean the names to ensure proper formatting for the dashboard display.

# task

Clean Sales Rep Names

# your code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
🐍
Loading PythonSetting up pandas & numpy...