Python String Methods
Python String Methods
1. str.capitalize()
Purpose: Converts the first character of the string to uppercase and the
rest to lowercase.
Syntax: string.capitalize()
Example:
2. str.lower()
Purpose: Converts all characters in the string to lowercase.
Syntax: string.lower()
Example:
3. str.upper()
Purpose: Converts all characters in the string to uppercase.
Example:
4. str.title()
Purpose: Converts the first character of each word to uppercase and the
rest to lowercase.
Syntax: string.title()
Example:
5. str.swapcase()
Purpose: Swaps the case of all characters in the string (uppercase
becomes lowercase and vice versa).
Syntax: string.swapcase()
Example:
6. str.strip()
Purpose: Removes leading and trailing whitespace (or specified characters)
from the string.
Syntax: string.strip([chars])
Example:
7. str.lstrip()
Purpose: Removes leading whitespace (or specified characters) from the
string.
Syntax: string.lstrip([chars])
Example:
8. str.rstrip()
Purpose: Removes trailing whitespace (or specified characters) from the
string.
Syntax: string.rstrip([chars])
Example:
9. str.replace()
Purpose: Replaces all occurrences of a substring with another substring.
Example:
10. str.split()
Example:
11. str.join()
Purpose: Joins elements of an iterable (e.g., list) into a single string using
the string as a separator.
Syntax: string.join(iterable)
Example:
12. str.find()
Purpose: Returns the lowest index of the substring if found, otherwise
returns 1 .
Example:
13. str.index()
Purpose: Similar to find() , but raises a ValueError if the substring is not found.
Example:
14. str.count()
Purpose: Returns the number of non-overlapping occurrences of a
substring in the string.
Example:
15. str.startswith()
Purpose: Checks if the string starts with a specified prefix.
Example:
16. str.endswith()
Purpose: Checks if the string ends with a specified suffix.
Example:
17. str.isalpha()
Purpose: Checks if all characters in the string are alphabetic (letters).
Example:
text = "hello"
print(text.isalpha()) # Output: True
18. str.isdigit()
Purpose: Checks if all characters in the string are digits.
Syntax: string.isdigit()
Example:
text = "123"
print(text.isdigit()) # Output: True
19. str.isalnum()
Purpose: Checks if all characters in the string are alphanumeric (letters or
digits).
Syntax: string.isalnum()
Example:
text = "hello123"
print(text.isalnum()) # Output: True
20. str.islower()
Purpose: Checks if all characters in the string are lowercase.
Syntax: string.islower()
Example:
text = "hello"
print(text.islower()) # Output: True
Syntax: string.isupper()
Example:
text = "HELLO"
print(text.isupper()) # Output: True
22. str.isspace()
Purpose: Checks if all characters in the string are whitespace.
Syntax: string.isspace()
Example:
23. str.zfill()
Purpose: Pads the string with zeros on the left until it reaches the specified
length.
Syntax: string.zfill(width)
Example:
text = "42"
print(text.zfill(5)) # Output: "00042"
24. str.format()
Purpose: Formats the string by replacing placeholders {} with specified
values.
Example:
25. str.center()
Purpose: Centers the string in a field of a specified width.
Example:
text = "hello"
print(text.center(10, "-")) # Output: "--hello---"
26. str.ljust()
Purpose: Left-justifies the string in a field of a specified width.
Example:
text = "hello"
print(text.ljust(10, "-")) # Output: "hello-----"
27. str.rjust()
Purpose: Right-justifies the string in a field of a specified width.
Example:
text = "hello"
print(text.rjust(10, "-")) # Output: "-----hello"
28. str.expandtabs()
Purpose: Replaces tab characters ( \t ) with spaces.
Example:
text = "hello\tworld"
print(text.expandtabs(4)) # Output: "hello world"
29. str.encode()
Purpose: Encodes the string into bytes using a specified encoding (default
is utf-8 ).
Example:
text = "hello"
print(text.encode()) # Output: b'hello'
30. str.translate()
Purpose: Translates the string using a translation table (created
with str.maketrans() ).
Syntax: string.translate(table)
Example:
text = "hello"
table = str.maketrans("el", "EL")
print(text.translate(table)) # Output: "hELLo"