-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path15_strings.py
More file actions
26 lines (20 loc) · 757 Bytes
/
15_strings.py
File metadata and controls
26 lines (20 loc) · 757 Bytes
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
# strings in Python are immutable
s = "Hello"
s[0] = "h" # ❌ This will raise an error
# --------
s = " Hello, Python! "
# 1. Case Conversion
print(s.upper()) # " HELLO, PYTHON! "
print(s.lower()) # " hello, python! "
print(s.strip()) # "Hello, Python!" (removes spaces)
# 2. Search and Replace
print(s.find("Python")) # 9 (index of first occurrence)
print(s.replace("Python", "World")) # " Hello, World! "
# 3. String Checking
print("hello".isalpha()) # True (all letters)
print("123".isdigit()) # True (all digits)
print("hello123".isalnum()) # True (letters + digits)
# 4. Splitting and Joining
words = s.split(",") # Splits at comma
print(words) # [' Hello', ' Python! ']
print("-".join(words)) # " Hello- Python! "