-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring_methods.py
More file actions
38 lines (31 loc) · 1.2 KB
/
string_methods.py
File metadata and controls
38 lines (31 loc) · 1.2 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
# -------------------------------------------
# String Case Manipulation
name = "rAnJItH jayadEVAN"
# Prints the original mixed-case name
print("Original Text: ", name)
# Converts entire string to UPPERCASE
print("Upper: ", name.upper())
# Converts entire string to lowercase
print("Lower: ", name.lower())
# Only first character becomes uppercase, rest lowercase
print("Capitalize: ", name.capitalize())
# Capitalizes first letter of every word
print("Title: ", name.title())
# Swaps case of each character: lowercase <-> UPPERCASE
print("Swap: ", name.swapcase())
# -------------------------------------------
# Whitespace Removal using Strip Methods
# String with leading and trailing spaces
text = " Hello "
# Shows whitespace before and after the word
print("Original Text:", text)
# Removes spaces from both ends
print("Strip :", text.strip())
# Removes spaces from the left (start)
print("LStrip :", text.lstrip())
# Removes spaces from the right (end)
print("RStrip :", text.rstrip())
# -------------------------------------------
# Get the length of the string including spaces
# len() counts all characters, including whitespace
print("Length (with spaces):", len(text))