Sitemap
Burra Chandana

Burra Chandana

PYTHON STRING METHODS

4 min readJun 25, 2026

--

Introduction

Strings are one of the most commonly used data types in Python. They help us store and work with text data such as names, messages, emails, and more. Python provides many built-in string methods that make text manipulation easy and efficient.

In this blog, we will learn some important Python string methods with simple explanations and examples.

1. upper()

The upper() method converts all characters in a string to uppercase.

Example:

Text: "python programming"

Result: "PYTHON PROGRAMMING"

2. lower()

The lower() method converts all characters in a string to lowercase.

Example:

Text: "PYTHON PROGRAMMING"

Result: "python programming"

3. swapcase()

The swapcase() method changes uppercase letters to lowercase and lowercase letters to uppercase.

Example:

Text: "PyThOn"

Result: "pYtHoN"

4. capitalize()

The capitalize() method converts the first character of the string to uppercase and the remaining characters to lowercase.

Example:

Text: "python is fun"

Result: "Python is fun"

5. title()

The title() method converts the first letter of every word to uppercase.

Example:

Text: "python programming language"

Result: "Python Programming Language"

6. count()

The count() method returns the number of times a substring appears in a string.

Example:

Text: "banana"

Count of "a" = 3

7. index()

The index() method returns the position of the first occurrence of a substring.

Example:

Text: "python"

Index of "t" = 2

8. find()

The find() method searches for a substring and returns its position.

Example:

Text: "python programming"

Find "programming" = 7

9. rindex()

The rindex() method returns the last occurrence position of a substring.

Example:

Text: "banana"

Last index of "a" = 5

10. rfind()

The rfind() method searches from the right side and returns the last occurrence position.

Example:

Text: "banana"

Last find of "a" = 5

11. replace()

The replace() method replaces one substring with another.

Example:

Text: "I like Java"

Replace "Java" with "Python"

Result: "I like Python"

12. strip()

The strip() method removes spaces from both sides of a string.

Example:

Text: " Python "

Result: "Python"

13. rstrip()

The rstrip() method removes spaces from the right side.

Example:

Text: "Python "

Result: "Python"

14. lstrip()

The lstrip() method removes spaces from the left side.

Example:

Text: " Python"

Result: "Python"

15. split()

The split() method breaks a string into a list using a separator.

Example:

Text: "Apple,Mango,Banana"

Split by ","

Result: ['Apple', 'Mango', 'Banana']

16. rsplit()

The rsplit() method splits a string starting from the right side.

Example:

Text: "one-two-three"

Split from right using "-"

Result: ['one', 'two', 'three']

17. partition()

The partition() method divides a string into three parts based on the first occurrence of a separator.

Example:

Get Burra Chandana’s stories in your inbox

Join Medium for free to get updates from this writer.

Text: "Python-Programming"

Partition by "-"

Result: ('Python', '-', 'Programming')

18. center()

The center() method places the string in the center of a specified width.

Example:

Text: "Python"

Width = 10

Result: " Python "

19. zfill()

The zfill() method adds zeros at the beginning of a string.

Example:

Text: "25"

Width = 5

Result: "00025"

20. maketrans()

The maketrans() method creates a translation table.

Example:

Convert:

a → 1

b → 2

Translation table created for these replacements.

21. translate()

The translate() method uses the translation table created by maketrans().

Example:

Text: "apple"

Using translation:

a → 1

Result: "1pple"

22. isupper()

The isupper() method returns True if all letters are uppercase.

Example:

Text: "PYTHON"

Result: True

23. islower()

The islower() method returns True if all letters are lowercase.

Example:

Text: "python"

Result: True

24. istitle()

The istitle() method returns True if every word starts with a capital letter.

Example:

Text: "Python Programming"

Result: True

25. isdigit()

The isdigit() method returns True if all characters are digits.

Example:

Text: "12345"

Result: True

26. isalpha()

The isalpha() method returns True if all characters are alphabets.

Example:

Text: "Python"

Result: True

27. isalnum()

The isalnum() method returns True if the string contains only letters and numbers.

Example:

Text: "Python123"

Result: True

28. isspace()

The isspace() method returns True if the string contains only spaces.

Example:

Text: " "

Result: True

29. isidentifier()

The isidentifier() method checks whether a string can be used as a valid Python variable name.

Example:

Text: "student_name"

Result: True

30. startswith()

The startswith() method checks whether a string starts with a specified value.

Example:

Text: "Python Programming"

Starts with "Python"

Result: True

31. endswith()

The endswith() method checks whether a string ends with a specified value.

Example:

Text: "Python Programming"

Ends with "Programming"

Result: True

32. count(substring, start, end)

This version of count() counts occurrences only within a specified range.

Example:

Text: "banana"

Count "a" between positions 1 and 5

Result: 2

Conclusion

Python string methods help us perform various text operations easily. We can convert text cases, search for words, replace content, remove spaces, split strings, validate data, and much more. Learning these string methods is important because they are widely used in real-world Python applications such as form validation, text processing, data cleaning, and automation tasks.

Mastering these methods will make your Python programming faster, cleaner, and more efficient.

--

--