-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.py
More file actions
283 lines (218 loc) · 9.49 KB
/
example.py
File metadata and controls
283 lines (218 loc) · 9.49 KB
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
"""
String Operations — Example Code
==================================
Run this file:
python3 example.py
This file demonstrates how to index, slice, search, modify, and format
strings in Python. Strings are one of the most-used types, so get
comfortable with these operations!
"""
# -----------------------------------------------------------------------------
# 1. Strings are sequences — indexing with []
# -----------------------------------------------------------------------------
print("--- 1. Indexing ---")
s = "Python"
# Each character has a position, starting at 0
print(f"String: {s!r}")
print(f"s[0] = {s[0]!r}") # P (first character)
print(f"s[1] = {s[1]!r}") # y
print(f"s[5] = {s[5]!r}") # n (last character)
print(f"Length: {len(s)}") # 6 characters total
print()
# -----------------------------------------------------------------------------
# 2. Negative indexing — count from the end
# -----------------------------------------------------------------------------
print("--- 2. Negative Indexing ---")
s = "Python"
print(f"s[-1] = {s[-1]!r}") # n (last character)
print(f"s[-2] = {s[-2]!r}") # o (second to last)
print(f"s[-6] = {s[-6]!r}") # P (same as s[0])
# Super useful when you just want the last character of any string
filename = "report.pdf"
extension = filename[-3:]
print(f"Extension of {filename!r}: {extension!r}")
print()
# -----------------------------------------------------------------------------
# 3. Slicing — grab chunks of a string
# -----------------------------------------------------------------------------
print("--- 3. Slicing ---")
s = "Hello, World!"
# Basic slicing: s[start:stop] (stop is NOT included)
print(f"s[0:5] = {s[0:5]!r}") # 'Hello'
print(f"s[7:12] = {s[7:12]!r}") # 'World'
# Omit start or stop
print(f"s[:5] = {s[:5]!r}") # 'Hello' (from beginning)
print(f"s[7:] = {s[7:]!r}") # 'World!' (to the end)
print(f"s[:] = {s[:]!r}") # 'Hello, World!' (full copy)
# Slicing with a step: s[start:stop:step]
alphabet = "abcdefghij"
print(f"Every 2nd: {alphabet[::2]!r}") # 'acegi'
print(f"Every 3rd: {alphabet[::3]!r}") # 'adgj'
# The classic — reverse a string
print(f"Reversed: {s[::-1]!r}") # '!dlroW ,olleH'
print()
# -----------------------------------------------------------------------------
# 4. String methods — Case
# -----------------------------------------------------------------------------
print("--- 4. Case Methods ---")
s = "hello, world"
print(f"Original: {s!r}")
print(f".upper(): {s.upper()!r}") # 'HELLO, WORLD'
print(f".lower(): {s.lower()!r}") # 'hello, world'
print(f".title(): {s.title()!r}") # 'Hello, World'
print(f".capitalize(): {s.capitalize()!r}") # 'Hello, world'
print(f".swapcase(): {s.swapcase()!r}") # 'HELLO, WORLD'
mixed = "hElLo WoRlD"
print(f"Swapcase of {mixed!r}: {mixed.swapcase()!r}") # 'HeLlO wOrLd'
print()
# -----------------------------------------------------------------------------
# 5. String methods — Search
# -----------------------------------------------------------------------------
print("--- 5. Search Methods ---")
s = "the quick brown fox jumps over the lazy dog"
# find() returns index of first match, or -1 if not found
print(f"find('fox'): {s.find('fox')}") # 16
print(f"find('cat'): {s.find('cat')}") # -1 (not found)
# index() is like find(), but raises an error if not found
print(f"index('fox'): {s.index('fox')}") # 16
# count() — how many times does a substring appear?
print(f"count('the'): {s.count('the')}") # 2
print(f"count('o'): {s.count('o')}") # 4
# startswith() and endswith()
print(f"startswith('the'): {s.startswith('the')}") # True
print(f"endswith('dog'): {s.endswith('dog')}") # True
print(f"endswith('cat'): {s.endswith('cat')}") # False
print()
# -----------------------------------------------------------------------------
# 6. String methods — Modify (strip, replace)
# -----------------------------------------------------------------------------
print("--- 6. Modify Methods ---")
# strip() removes leading/trailing whitespace
messy = " hello, world "
print(f"Original: {messy!r}")
print(f".strip(): {messy.strip()!r}") # 'hello, world'
print(f".lstrip(): {messy.lstrip()!r}") # 'hello, world '
print(f".rstrip(): {messy.rstrip()!r}") # ' hello, world'
# Strip specific characters
dashes = "---hello---"
print(f"Strip dashes: {dashes.strip('-')!r}") # 'hello'
# replace() swaps one substring for another
s = "I like cats and cats like me"
print(f"Original: {s!r}")
print(f"Replace cats->dogs: {s.replace('cats', 'dogs')!r}")
print(f"Replace first only: {s.replace('cats', 'dogs', 1)!r}")
print()
# -----------------------------------------------------------------------------
# 7. String methods — Check (is-methods)
# -----------------------------------------------------------------------------
print("--- 7. Check Methods ---")
# isdigit() — all digits?
print(f"'12345'.isdigit(): {'12345'.isdigit()}") # True
print(f"'12.45'.isdigit(): {'12.45'.isdigit()}") # False
# isalpha() — all letters?
print(f"'Hello'.isalpha(): {'Hello'.isalpha()}") # True
print(f"'Hello!'.isalpha(): {'Hello!'.isalpha()}") # False
# isalnum() — letters or digits?
print(f"'abc123'.isalnum(): {'abc123'.isalnum()}") # True
print(f"'abc 12'.isalnum(): {'abc 12'.isalnum()}") # False
# isspace() — all whitespace?
print(f"' '.isspace(): {' '.isspace()}") # True
print(f"''.isspace(): {''.isspace()}") # False
print()
# -----------------------------------------------------------------------------
# 8. String methods — Split and Join
# -----------------------------------------------------------------------------
print("--- 8. Split and Join ---")
# split() breaks a string into a list
sentence = "the quick brown fox"
words = sentence.split()
print(f"Split: {sentence!r} -> {words}")
# Split on a specific delimiter
date = "2025-02-09"
parts = date.split("-")
print(f"Split date: {date!r} -> {parts}")
csv_line = "Alice,30,Engineer"
fields = csv_line.split(",")
print(f"Split CSV: {csv_line!r} -> {fields}")
# join() glues a list back together
words = ["Hello", "World"]
print(f"Join with space: {' '.join(words)!r}") # 'Hello World'
print(f"Join with dash: {'-'.join(words)!r}") # 'Hello-World'
print(f"Join with empty: {''.join(words)!r}") # 'HelloWorld'
# The split -> modify -> join pattern
sentence = "python is awesome"
title_version = " ".join(word.capitalize() for word in sentence.split())
print(f"Capitalized words: {title_version!r}") # 'Python Is Awesome'
print()
# -----------------------------------------------------------------------------
# 9. Strings are immutable — you can't change them in place
# -----------------------------------------------------------------------------
print("--- 9. Immutability ---")
s = "Hello"
# s[0] = "h" # This would raise: TypeError: 'str' object does not support item assignment
# Instead, create a new string
new_s = "h" + s[1:]
print(f"Original: {s!r}")
print(f"New: {new_s!r}")
# Methods don't change the original either
name = "alice"
upper_name = name.upper()
print(f"name is still: {name!r}")
print(f"upper_name: {upper_name!r}")
print()
# -----------------------------------------------------------------------------
# 10. f-string formatting — alignment, padding, numbers
# -----------------------------------------------------------------------------
print("--- 10. f-string Formatting ---")
# Alignment: < (left), > (right), ^ (center)
print(f"|{'left':<15}|") # |left |
print(f"|{'right':>15}|") # | right|
print(f"|{'center':^15}|") # | center |
# Fill character + alignment
print(f"{'hello':*^20}") # *******hello********
print(f"{'hello':->20}") # ---------------hello
print(f"{'hello':.>20}") # ...............hello
# Zero-padded numbers
print(f"Order #{42:05d}") # Order #00042
print(f"Code: {7:03d}") # Code: 007
# Decimal precision
pi = 3.14159265
print(f"Pi (2 decimals): {pi:.2f}") # 3.14
print(f"Pi (4 decimals): {pi:.4f}") # 3.1416
# Thousands separator
big = 1_234_567
print(f"Population: {big:,}") # Population: 1,234,567
# Percentages
ratio = 0.856
print(f"Score: {ratio:.1%}") # Score: 85.6%
# Combining width and format
for item, price in [("Coffee", 4.5), ("Muffin", 3.25), ("Juice", 5.0)]:
print(f" {item:<10} ${price:>6.2f}")
print()
# -----------------------------------------------------------------------------
# 11. Raw strings — literal backslashes
# -----------------------------------------------------------------------------
print("--- 11. Raw Strings ---")
# Normal string: \t is a tab, \n is a newline
print("Normal: hello\tworld") # hello world (tab in the middle)
# Raw string: backslashes are just backslashes
print(r"Raw: hello\tworld") # hello\tworld (literal characters)
# Useful for file paths (especially on Windows)
normal_path = "C:\\Users\\Alice\\Documents"
raw_path = r"C:\Users\Alice\Documents"
print(f"Normal path: {normal_path}")
print(f"Raw path: {raw_path}")
# Useful for regex patterns
import re
pattern = r"\d{3}-\d{3}-\d{4}"
phone = "Call me at 555-123-4567 today"
match = re.search(pattern, phone)
if match:
print(f"Found phone number: {match.group()}")
print()
# -----------------------------------------------------------------------------
# Done!
# -----------------------------------------------------------------------------
print("=" * 40)
print(" STRING OPERATIONS COMPLETE!")
print("=" * 40)