-
Notifications
You must be signed in to change notification settings - Fork 1
/
string_in_python.py
48 lines (31 loc) · 1.54 KB
/
string_in_python.py
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
# Example of string methods
# Initial string
text = " Hello World Python "
# Convert to lowercase
print(text.lower()) # Output: " hello world python "
# Convert to uppercase
print(text.upper()) # Output: " HELLO WORLD PYTHON "
# Capitalize each word
print(text.title()) # Output: " Hello World Python "
# Strip leading and trailing spaces
print(text.strip()) # Output: "Hello World Python"
# Replace 'Python' with 'Programming'
print(text.replace("Python", "Programming")) # Output: " Hello World Programming "
# Split the string into a list of words
print(text.split()) # Output: ['Hello', 'World', 'Python']
# Join a list of words into a single string with spaces
words = ['Hello', 'World', 'Python']
print(" ".join(words)) # Output: "Hello World Python"
# Find the position of 'World'
print(text.find("World")) # Output: 11
# Check if the string starts with ' Hello'
print(text.startswith(" Hello")) # Output: True
# Check if the string ends with 'Python'
print(text.endswith("Python ")) # Output: True
# Check if the string contains only digits
print("12345".isdigit()) # Output: True
# Check if the string contains only alphabetic characters
print("Hello".isalpha()) # Output: True
### Key Points
# - **Strings are immutable:** Methods that modify a string return a new string rather than changing the original string.
# - **Method chaining:** You can chain methods together for more complex operations. For example, `text.strip().upper().replace("WORLD", "EVERYONE")`.