STRINGS IN PYTHON Methods CODING PROGRAMS
# Sample string
text = "Hello World"
num_text = "12345"
mixed_text = "Python3"
1. capitalize()
print("python".capitalize()) # Output: 'Python'
Converts the first character to uppercase and the rest to lowercase.
2. casefold()
print("PYTHON".casefold()) # Output: 'python'
Converts string to lowercase (more aggressive than lower() for special
characters).
3. center(width)
print("Python".center(20)) # Output: ' Python '
Centers the string within the given width, padding with spaces.
4. count(substring)
print("banana".count("a")) # Output: 3
Counts occurrences of a substring.
5. endswith(suffix)
print("[Link]".endswith(".txt")) # Output: True
Checks if the string ends with the given suffix.
6. expandtabs(tabsize)
print("Hello\tWorld".expandtabs(10)) # Output: 'Hello World'
Replaces \t with spaces (default tab size = 8).
7. find(substring)
print("Python".find("th")) # Output: 2
Returns the index of the first occurrence, or -1 if not found.
8. format()
print("My name is {}".format("Alice")) # Output: 'My name is Alice'
Formats strings using placeholders {}.
9. format_map(mapping)
data = {"name": "Alice", "age": 25}
print("{name} is {age} years old".format_map(data))
# Output: 'Alice is 25 years old'
Similar to format(), but uses a dictionary directly.
10. index(substring)
print("Python".index("th")) # Output: 2
Like find(), but raises an error if substring not found.
11. isalnum()
print("Python3".isalnum()) # Output: True
Returns True if all characters are alphanumeric.
12. isalpha()
print("Python".isalpha()) # Output: True
Returns True if all characters are letters.
13. isdecimal()
print("123".isdecimal()) # Output: True
Returns True if all characters are decimal digits.
14. isdigit()
print("123".isdigit()) # Output: True
Returns True if all characters are digits (includes superscripts, etc.).
15. isidentifier()
print("variable1".isidentifier()) # Output: True
print("123abc".isidentifier()) # Output: False
Checks if the string is a valid Python identifier.
16. islower()
print("python".islower()) # Output: True
Returns True if all characters are lowercase.
17. isnumeric()
print("Ⅷ".isnumeric()) # Output: True
Returns True if all characters are numeric (includes fractions, Roman
numerals, etc.).
18. isprintable()
print("Hello".isprintable()) # Output: True
print("\n".isprintable()) # Output: False
Checks if all characters are printable.
19. isspace()
print(" ".isspace()) # Output: True
Returns True if all characters are whitespace.
20. istitle()
print("Hello World".istitle()) # Output: True
Returns True if each word starts with uppercase followed by
lowercase.
21. isupper()
print("PYTHON".isupper()) # Output: True
Returns True if all characters are uppercase.
22. join(iterable)
print("-".join(["a", "b", "c"])) # Output: 'a-b-c'
Joins elements of an iterable with the string as separator.
23. ljust(width)
print("Python".ljust(10, "*")) # Output: 'Python****'
Left-aligns string within given width, padding with chosen character.
24. lstrip()
print(" Python".lstrip()) # Output: 'Python'
Removes leading whitespace (or specified characters).
25. replace(old, new)
print("Hello World".replace("World", "Python")) # Output: 'Hello Python'
Replaces all occurrences of a substring with another.