Python String Methods


Python string methods are built-in functions that allow you to perform various operations on strings. These methods help you modify, analyze, and transform strings efficiently.


1. upper() – Convert to Uppercase

Converts all characters in a string to uppercase.

text = "python"
print(text.upper())

Output:

PYTHON


2. lower() – Convert to Lowercase

Converts all characters to lowercase.

text = "PyThOn"
print(text.lower())

Output:

python


3. capitalize() – Capitalize First Letter

Capitalizes only the first character of the string.

text = "python is fun"
print(text.capitalize())

Output:

Python is fun


4. title() – Capitalize First Letter of Each Word

Capitalizes the first letter of every word.

text = "python is awesome"
print(text.title())

Output:

Python Is Awesome


5. strip() – Remove Leading and Trailing Spaces

Removes whitespace from both ends of the string.

text = "  hello world  "
print(text.strip())

Output:

hello world


6. lstrip() and rstrip() – Remove Spaces from Left/Right

text = "  hello  "
print(text.lstrip())   # Removes from left
print(text.rstrip())   # Removes from right

Output:

hello  
  hello


7. replace() – Replace Substring

Replaces all occurrences of a substring with another.

text = "I like apples"
print(text.replace("apples", "bananas"))

Output:

I like bananas


8. split() – Split String into List

Splits the string by spaces (or any delimiter) and returns a list.

text = "python is easy"
print(text.split())

Output:

['python', 'is', 'easy']


9. join() – Join List into String

Joins elements of a list into a single string.

words = ['Python', 'is', 'fun']
print(" ".join(words))

Output:

Python is fun


10. find() – Return First Index of Substring

Returns the index of the first occurrence of a substring. Returns -1 if not found.

text = "hello world"
print(text.find("world"))

Output:

6


11. index() – Like find() but Raises Error if Not Found

text = "hello"
print(text.index("e"))

Output:

1

If the substring doesn't exist, index() throws a ValueError.



12. count() – Count Occurrences

Counts how many times a substring appears.

text = "banana"
print(text.count("a"))

Output:

3


13. startswith() and endswith() – Check Prefix/Suffix

text = "hello world"
print(text.startswith("hello"))  # True
print(text.endswith("world"))    # True

Output:

True
True


14. isalpha() – All Alphabet Characters?

print("abc".isalpha())  # True
print("abc123".isalpha())  # False


15. isdigit() – All Digits?

print("123".isdigit())  # True
print("abc123".isdigit())  # False


16. isalnum() – All Alphanumeric?

print("abc123".isalnum())  # True
print("abc 123".isalnum())  # False


17. isspace() – All Whitespace?

print("   ".isspace())  # True
print(" a ".isspace())  # False


18. swapcase() – Swap Upper to Lower and Vice Versa

text = "PyThOn"
print(text.swapcase())

Output:

pYtHoN


19. zfill(width) – Pad with Zeros

Pads the string on the left with zeros to reach the given width.

text = "42"
print(text.zfill(5))

Output:

00042


20. format() – String Formatting

Inserts variables into string templates.

name = "John"
age = 25
print("My name is {} and I am {} years old.".format(name, age))

Output:

My name is John and I am 25 years old.


Bonus: f-string – Modern Python String Formatting (Python 3.6+)

name = "Alice"
age = 30
print(f"My name is {name} and I am {age} years old.")

Output:

My name is Alice and I am 30 years old.