String Methods In Python- Part 1

We’ve learned before about the string, and we said it is a sequence of characters or a word in a simple term, but we didn’t explore their methods. Python developer has built some methods that work with string to make changes to these characters or extend their functionalities. This article will explore some of these methods.

1. Capitalize() Function

In the English language, the first sentence of any phrase should be capitalized. There is a built-in python function that works with a string called capitalize() method that can make the first sentence of your phrase capital. Let’s see an example:

# Create a Text
text = "this is an example"

# Capitalize The Text
capitalize = text.capitalize()

# Print The Text
print(capitalize)

You will see the first latter of this phrase which is “t” in the “this” word, will be capital, but the other words won’t be affected by this method.

2. Center() Method

The center() method will allow you to take a space in the front & after the word. The space is will counted by characters, and let me show you an example to understand:

# Create The Text
text = "is"

# Center The Text
text = text.center(10)

# Print The Text
print("This", text, "an example")

We’ve specified the value 10 inside the center() methods, which means it will take 10 characters in the before and after the word, as you see from the result when you execute the code. The center() method can also take another parameter. Let’s see this example:

# Create The Text
text = "is"

# Center The Text
text = text.center(10, "*")

# Print The Text
print("This", text, "an example")

We’ve specified the “*” asterisk symbol as a second parameter, and it will fill out the space in the before and after the word with that symbol. Feel free to change the symbol to anything you want.

3. Count() Method

You can count the number of times a particular word appears in the text. This function is called count() and can be helpful when you learn natural language processing. Let’s see an example:

# Create The Text
text = "I love honey, because honey can boost you immunity system"

# Count a Specific Word
count = text.count("honey")

# Print The Count
print(count)

It will give you result 2 because the word “honey” occurs twice inside the text. This method will not count the word “honey” if, for example, one of the characters is capital. Even if they are the same word, the small and capital characters make the difference. 

The count() method also can take two other optional parameters, which will specify where in the text to start the search and stop. Let’s see an example:

# Create The Text
text = "I love honey. honey can boost you immunity system"

# Count a Specific Word
count = text.count("honey", 10, 30)

# Print The Count
print(count)

We’ve specified when to start and stop the search, and it will give you a different result, and it will be 1 at this time.

4. Endswith() Method

You can check if a particular sentence or phrase ends with a specific word using the endswith() python method. Let’s see an example:

# Create The Text
text = "I love honey. honey can boost you immunity system"

# Check If The Text Ends With a Specific Word
result = text.endswith("system")

# Print The Result
print(result)

The endswith() method will return a boolean, which is True in this example since the lest text ends with the “system” word. You can specify where to start searching and where to stop like the previous method. Let’s see an example:

# Create The Text
text = "I love honey. honey can boost you immunity system"

# Check If The Text Ends With a Specific Word
count = text.endswith("system", 0, 10)

# Print The Result
print(count)

You will get the False result since the last word when the search ends is not the “system” word. 

5. Startswith() Method

There is also a similar method, but it checks if the text starts with a particular word. Let me show you an example:

# Create The Text
text = "I love honey. honey can boost you immunity system"

# Check If The Text Starts With a Specific Word
result = text.startswith("system")

# Print The Result
print(result)

It will give you a False result since the text doesn’t start with the “system” word, and you can notice that we’ve used the startswith() method. We can also tell this method where to start searching and stop:

# Create The Text
text = "I love honey. honey can boost you immunity system"

# Check If The Text Starts With a Specific Word
count = text.startswith("I", 0, 10)

# Print The Result
print(count)

This time you will get the True as a result when you execute the code because it will start searching from the first character, and the first one is the “I” character.

6. Find() Method

You can get the index of any word inside your sentence using the find() method in python. Let’s see an example:

# Create The Text
text = 'I love honey'

# Get The Word Index
result = text.find("honey")

# Print The Result
print(result)

You will get the value 7 in the result, meaning the word “honey” starting at the index 7 of your text. If, for example, it didn’t find the word, it will return the -1 value instead of raising an error.

Conclusion

Thanks for reading! String methods can help you make changes to your string in your python code, and there are a lot of them, not just the ones in this article. We will look at more of them in the following tutorial.

Scroll to Top