🐍 Comprehensive Analysis of Python String Methods and Operators
String manipulation is one of the most commonly used features in Python programming. This article will provide a comprehensive overview of the built-in methods and various operators for Python strings, helping you efficiently handle text data and perform various calculations.
1. Python String Methods
Python offers a rich set of string manipulation methods, all of which do not modify the original string but return a new string.
1. Case Conversion
| Method | Description | Example |
|---|---|---|
<span>capitalize()</span> |
Capitalize the first letter | <span>"hello".capitalize()</span> → “Hello” |
<span>lower()</span> |
Convert to lowercase | <span>"HELLO".lower()</span> → “hello” |
<span>upper()</span> |
Convert to uppercase | <span>"hello".upper()</span> → “HELLO” |
<span>title()</span> |
Capitalize the first letter of each word | <span>"hello world".title()</span> → “Hello World” |
<span>swapcase()</span> |
Swap case | <span>"Hello".swapcase()</span> → “hELLO” |
2. Find and Replace
| Method | Description | Example |
|---|---|---|
<span>find()</span> |
Find the position of a substring | <span>"hello".find("e")</span> → 1 |
<span>index()</span> |
Find the position of a substring | <span>"hello".index("e")</span> → 1 |
<span>count()</span> |
Count occurrences of a substring | <span>"hello".count("l")</span> → 2 |
<span>replace()</span> |
Replace a substring | <span>"hello".replace("l", "x")</span> → “hexxo” |
3. String Checks
| Method | Description | Example |
|---|---|---|
<span>startswith()</span> |
Check if the string starts with a specified substring | <span>"hello".startswith("he")</span> → True |
<span>endswith()</span> |
Check if the string ends with a specified substring | <span>"hello".endswith("lo")</span> → True |
<span>isalpha()</span> |
Check if the string contains only alphabetic characters | <span>"hello".isalpha()</span> → True |
<span>isdigit()</span> |
Check if the string contains only digits | <span>"123".isdigit()</span> → True |
4. String Formatting
Example code
name = "Alice"
age = 25
# Using format method
print("My name is {} and I'm {} years old".format(name, age))
# Using f-string (Python 3.6+)
print(f"My name is {name} and I'm {age} years old")
2. Python Operators
Python provides various operators for different calculations and comparison operations.
1. Arithmetic Operators
| Operator | Description | Example |
|---|---|---|
<span>+</span> |
Addition | <span>3 + 5</span> → 8 |
<span>-</span> |
Subtraction | <span>5 - 3</span> → 2 |
<span>*</span> |
Multiplication | <span>3 * 5</span> → 15 |
<span>/</span> |
Division | <span>10 / 2</span> → 5.0 |
<span>%</span> |
Modulus | <span>10 % 3</span> → 1 |
<span>**</span> |
Exponentiation | <span>2 ** 3</span> → 8 |
<span>//</span> |
Floor Division | <span>10 // 3</span> → 3 |
2. Comparison Operators
| Operator | Description | Example |
|---|---|---|
<span>==</span> |
Equal to | <span>5 == 5</span> → True |
<span>!=</span> |
Not equal to | <span>5 != 3</span> → True |
<span>></span> |
Greater than | <span>5 > 3</span> → True |
<span><</span> |
Less than | <span>3 < 5</span> → True |
<span>>=</span> |
Greater than or equal to | <span>5 >= 5</span> → True |
<span><=</span> |
Less than or equal to | <span>3 <= 5</span> → True |
3. Logical Operators
| Operator | Description | Example |
|---|---|---|
<span>and</span> |
Logical AND | <span>True and False</span> → False |
<span>or</span> |
Logical OR | <span>True or False</span> → True |
<span>not</span> |
Logical NOT | <span>not True</span> → False |
4. Assignment Operators
Example code
x = 5
x += 3 # Equivalent to x = x + 3
x -= 2 # Equivalent to x = x - 2
x *= 4 # Equivalent to x = x * 4
x /= 2 # Equivalent to x = x / 2
5. Membership Operators
Example code
fruits = ["apple", "banana", "cherry"]
print("apple" in fruits) # Output: True
print("orange" not in fruits) # Output: True