Python String Methods

Python provides a variety of built-in string methods that allow you to manipulate and work with strings. These methods help you perform tasks like finding substrings, replacing text, converting cases, and more. By learning these methods, you can efficiently handle string operations in your Python programs.

Let’s dive into some of the most commonly used Python string methods:

1. str.upper() and str.lower()

The str.upper() the method used to convert a string to uppercase and str.lower() method used to convert string to lowercase.

For example :

text = "Hello, World!"
uppercase_text = text.upper()
lowercase_text = text.lower()

print(uppercase_text)  
print(lowercase_text)  

Output

HELLO, WORLD!
hello, world!

2. str.strip()

The strip() method removes leading and trailing whitespace characters (spaces, tabs, newlines) from a string. It’s particularly handy when you’re processing user input or cleaning up data.

For example :

text = "   Python String Methods   "
cleaned_text = text.strip()

print(cleaned_text) 

Output

Python String Methods

We can see that it removed leading and trailing spaces.

3. str.split()

This method splits a string into a list of substrings based on a specified delimiter. It’s frequently used for parsing text and working with CSV or TSV files.

For example :

csv_data = "John,Doe,30"
data_list = csv_data.split(',')

print(data_list)

Output

['John', 'Doe', '30']

4. str.join()

The join() method is the opposite of split(). The str.join() method is used to concatenate elements of a string iterable into a new string, using the string as a separator.

words = ["Python", "is", "awesome"]
sentence = " ".join(words)

print(sentence) 

Output

Python is awesome

5. str.replace()

The str.replace() method is used to replace occurrences of a substring within a string with a new substring. It takes two arguments: the substring to be replaced and the new substring.

For example:

text = "I love programming in JavaScript."
modified_text = text.replace("JavaScript", "Python")

print(modified_text)  

Output

I love programming in Python.

6. str.find() and str.index()

Both methods are used to search for a substring within a string.

The str.find method returns the index of the first occurrence of a substring within a string. If the substring is not found, it returns -1. It takes an optional start and end index to search within.

The str.index() method is used to find the index of the first occurrence of a substring in a string. It raises a ValueError if the substring is not found.

text = "Python is a powerful language."
position1 = text.find("is")  
position2 = text.index("Java")  # Raises ValueError

Output

7

7. str.startswith() and str.endswith()

The str.startswith() method checks if a string starts with a specified substring. It returns True if the string starts with the substring, and False otherwise. The method takes the substring as an argument and is case-sensitive.

For example:

text = "www.example.com"
starts_with_www = text.startswith("www")

Here “starts_with_www ” variable holds the True value.

The str.endswith() method checks if a string ends with a specified suffix. It returns True if the string ends with the suffix, and False otherwise.

For example :

text = "www.example.com"
ends_with_net = text.endswith("net") 

Here “ends_with_net” variable holds the False value.

Conclusion

There you have it different string methods used in Python Programming. Python String Find Substring – Check String Contains Substring.