Python String Methods
1. [Link]()
Purpose: Converts the first character of the string to uppercase and the
rest to lowercase.
Syntax: [Link]()
Example:
text = "hello world"
print([Link]()) # Output: "Hello world"
2. [Link]()
Purpose: Converts all characters in the string to lowercase.
Syntax: [Link]()
Example:
text = "HELLO WORLD"
print([Link]()) # Output: "hello world"
3. [Link]()
Purpose: Converts all characters in the string to uppercase.
Python String Methods 1
Syntax: [Link]()
Example:
text = "hello world"
print([Link]()) # Output: "HELLO WORLD"
4. [Link]()
Purpose: Converts the first character of each word to uppercase and the
rest to lowercase.
Syntax: [Link]()
Example:
text = "hello world"
print([Link]()) # Output: "Hello World"
5. [Link]()
Purpose: Swaps the case of all characters in the string (uppercase
becomes lowercase and vice versa).
Syntax: [Link]()
Example:
text = "Hello World"
print([Link]()) # Output: "hELLO wORLD"
6. [Link]()
Purpose: Removes leading and trailing whitespace (or specified characters)
from the string.
Syntax: [Link]([chars])
Example:
Python String Methods 2
text = " hello world "
print([Link]()) # Output: "hello world"
7. [Link]()
Purpose: Removes leading whitespace (or specified characters) from the
string.
Syntax: [Link]([chars])
Example:
text = " hello world "
print([Link]()) # Output: "hello world "
8. [Link]()
Purpose: Removes trailing whitespace (or specified characters) from the
string.
Syntax: [Link]([chars])
Example:
text = " hello world "
print([Link]()) # Output: " hello world"
9. [Link]()
Purpose: Replaces all occurrences of a substring with another substring.
Syntax: [Link](old, new[, count])
Example:
text = "hello world"
print([Link]("world", "Python")) # Output: "hello Python"
10. [Link]()
Python String Methods 3
Purpose: Splits the string into a list of substrings based on a delimiter.
Syntax: [Link]([sep[, maxsplit]])
Example:
text = "hello world"
print([Link]()) # Output: ['hello', 'world']
11. [Link]()
Purpose: Joins elements of an iterable (e.g., list) into a single string using
the string as a separator.
Syntax: [Link](iterable)
Example:
words = ["hello", "world"]
print(" ".join(words)) # Output: "hello world"
12. [Link]()
Purpose: Returns the lowest index of the substring if found, otherwise
returns 1 .
Syntax: [Link](sub[, start[, end]])
Example:
text = "hello world"
print([Link]("world")) # Output: 6
13. [Link]()
Purpose: Similar to find() , but raises a ValueError if the substring is not found.
Syntax: [Link](sub[, start[, end]])
Example:
Python String Methods 4
text = "hello world"
print([Link]("world")) # Output: 6
14. [Link]()
Purpose: Returns the number of non-overlapping occurrences of a
substring in the string.
Syntax: [Link](sub[, start[, end]])
Example:
text = "hello world"
print([Link]("l")) # Output: 3
15. [Link]()
Purpose: Checks if the string starts with a specified prefix.
Syntax: [Link](prefix[, start[, end]])
Example:
text = "hello world"
print([Link]("hello")) # Output: True
16. [Link]()
Purpose: Checks if the string ends with a specified suffix.
Syntax: [Link](suffix[, start[, end]])
Example:
text = "hello world"
print([Link]("world")) # Output: True
17. [Link]()
Purpose: Checks if all characters in the string are alphabetic (letters).
Python String Methods 5
Syntax: [Link]()
Example:
text = "hello"
print([Link]()) # Output: True
18. [Link]()
Purpose: Checks if all characters in the string are digits.
Syntax: [Link]()
Example:
text = "123"
print([Link]()) # Output: True
19. [Link]()
Purpose: Checks if all characters in the string are alphanumeric (letters or
digits).
Syntax: [Link]()
Example:
text = "hello123"
print([Link]()) # Output: True
20. [Link]()
Purpose: Checks if all characters in the string are lowercase.
Syntax: [Link]()
Example:
text = "hello"
print([Link]()) # Output: True
Python String Methods 6
21. [Link]()
Purpose: Checks if all characters in the string are uppercase.
Syntax: [Link]()
Example:
text = "HELLO"
print([Link]()) # Output: True
22. [Link]()
Purpose: Checks if all characters in the string are whitespace.
Syntax: [Link]()
Example:
text = " "
print([Link]()) # Output: True
23. [Link]()
Purpose: Pads the string with zeros on the left until it reaches the specified
length.
Syntax: [Link](width)
Example:
text = "42"
print([Link](5)) # Output: "00042"
24. [Link]()
Purpose: Formats the string by replacing placeholders {} with specified
values.
Syntax: [Link](*args, **kwargs)
Example:
Python String Methods 7
text = "Hello, {}!"
print([Link]("world")) # Output: "Hello, world!"
25. [Link]()
Purpose: Centers the string in a field of a specified width.
Syntax: [Link](width[, fillchar])
Example:
text = "hello"
print([Link](10, "-")) # Output: "--hello---"
26. [Link]()
Purpose: Left-justifies the string in a field of a specified width.
Syntax: [Link](width[, fillchar])
Example:
text = "hello"
print([Link](10, "-")) # Output: "hello-----"
27. [Link]()
Purpose: Right-justifies the string in a field of a specified width.
Syntax: [Link](width[, fillchar])
Example:
text = "hello"
print([Link](10, "-")) # Output: "-----hello"
28. [Link]()
Purpose: Replaces tab characters ( \t ) with spaces.
Python String Methods 8
Syntax: [Link](tabsize=8)
Example:
text = "hello\tworld"
print([Link](4)) # Output: "hello world"
29. [Link]()
Purpose: Encodes the string into bytes using a specified encoding (default
is utf-8 ).
Syntax: [Link](encoding="utf-8", errors="strict")
Example:
text = "hello"
print([Link]()) # Output: b'hello'
30. [Link]()
Purpose: Translates the string using a translation table (created
with [Link]() ).
Syntax: [Link](table)
Example:
text = "hello"
table = [Link]("el", "EL")
print([Link](table)) # Output: "hELLo"
Python String Methods 9