-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPRacString.py
More file actions
36 lines (26 loc) · 1.45 KB
/
Copy pathPRacString.py
File metadata and controls
36 lines (26 loc) · 1.45 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
# Sample string
my_string = "hello world"
# capitalize() - Capitalizes the first letter
print("capitalize:", my_string.capitalize()) # "Hello world"
# center(width, char) - Centers the string within a specified width, filling with a character
print("center:", my_string.center(30, "-")) # "----hello world----"
# count(substring) - Counts occurrences of a substring
print("count 'l':", my_string.count("l")) # 3
# format() - Formats a string using placeholders
formatted_string = "My name is {} and I am {} years old.".format("Alice", 25)
print("format:", formatted_string) # "My name is Alice and I am 25 years old."
# index(substring) - Returns the index of the first occurrence of a substring
print("index 'world':", my_string.index("world")) # 6
# isdigit() - Checks if the string consists only of digits
print("isdigit:", "12345".isdigit()) # True
print("isdigit:", "123a45".isdigit()) # False
# isupper() - Checks if all characters in the string are uppercase
print("isupper:", "HELLO".isupper()) # True
print("isupper:", my_string.isupper()) # False
# istitle() - Checks if the string is in title case (Each word starts with uppercase)
print("istitle:", "Hello World".istitle()) # True
print("istitle:", my_string.istitle()) # False
# strip() - Removes leading and trailing whitespace
print("strip:", " hello ".strip()) # "hello"
# swapcase() - Swaps uppercase to lowercase and vice versa
print("swapcase:", "Hello WoRLd".swapcase()) # "hELLO wOrld"