Last modified: Oct 18, 2024 By Alexander Williams

Python String Methods

Strings are a fundamental data type in Python, and they come with many built-in methods that make manipulation easy. Whether you want to capitalize letters, find substrings, or modify strings, Python offers a wide array of methods. This article will explore some of the most commonly used Python string methods with examples.

1. capitalize()

The capitalize() method converts the first character of a string to uppercase while making the rest of the characters lowercase.


text = "hello world"
capitalized_text = text.capitalize()
print(capitalized_text)


Hello world

This method is particularly useful when you want to ensure the first letter of a sentence is capitalized.

2. upper() and lower()

The upper() method converts all characters in a string to uppercase, while the lower() method converts them all to lowercase.


text = "Python Programming"
print(text.upper())
print(text.lower())


PYTHON PROGRAMMING
python programming

If you want to check if a string contains any uppercase letters, check out Python String Uppercase | Check if Uppercase Exists.

3. strip(), lstrip(), and rstrip()

These methods are used to remove whitespace from strings. strip() removes whitespace from both ends, lstrip() removes it from the left, and rstrip() removes it from the right.


text = "   Python   "
print(text.strip())   # "Python"
print(text.lstrip())  # "Python   "
print(text.rstrip())  # "   Python"

4. replace()

The replace() method is used to replace occurrences of a specified substring with another substring.


text = "I love Python programming"
new_text = text.replace("Python", "Java")
print(new_text)


I love Java programming

5. find() and index()

The find() method returns the index of the first occurrence of a specified substring. If the substring is not found, it returns -1. The index() method is similar but raises an exception if the substring is not found.


text = "Learn Python programming"
position = text.find("Python")
print(position)


6

For more advanced searches, such as finding the shortest or longest word in a string, check out Python: Find Shortest or Longest Word in String.

6. split() and join()

The split() method splits a string into a list based on a specified delimiter, while join() is used to combine a list of strings into a single string with a specified delimiter.


text = "Python,Java,C++"
languages = text.split(",")
print(languages)

joined_text = "-".join(languages)
print(joined_text)


['Python', 'Java', 'C++']
Python-Java-C++

These methods are handy for working with CSV data or transforming text into lists.

7. startswith() and endswith()

The startswith() and endswith() methods are used to check if a string starts or ends with a specified substring.


text = "Hello, World!"
print(text.startswith("Hello"))  # True
print(text.endswith("World!"))   # True

8. len() - Getting the Length of a String

The len() function returns the number of characters in a string.


text = "Python"
print(len(text))


6

For example, to remove the last word from a string, you can use a combination of split() and join(). For more details, read Remove First or Last Word From String Python.

Conclusion

Python's string methods provide powerful ways to manipulate and work with text. Whether you are capitalizing letters, finding substrings, or splitting strings, these methods simplify many common tasks in programming. Understanding these methods will help you become more efficient when dealing with text data in Python.

If you're looking to explore more string manipulations, check out Python: Add Variable to String & Print Using 4 Methods to learn how to include variables in strings.