String Methods In Python- Part 3

String methods are some of the built-in python functions that will work only with strings, and they will extend their functionalities or change the text and check if a specific text has a particular thing. This article is complete for the previous article about string methods in python.

1. Upper() Method

This upper() method will return the whole given text to an upper case. It means making all of the characters in the upper case. Let’s see an example:

# Create a Text
text = "i love honey"

# Upper The Text
upper = text.upper()

# Print The Text
print(upper)

The text in this example will be like, “I LOVE HONEY”. It is similar to the capitalize() function, but it will change all the characters instead of changing only the first character of your text.

2. Lower() Method

The name tells you what it does. This lower() method will make the whole text in the lower case, not only the first character. It is the opposite of the upper() method. Let me show you an example:

# Create a Text
text = "I LOVE HONEY"

# Lower The Text
lower = text.lower()

# Print The Text
print(lower)

The text will be like “i love honey” where all characters become in lower case.

3. Isupper() Method

This isupper() method is very simple and will return Trus if all the given text is in the upper case. Otherwise, it will return False if only one character is lower. Let us try it out and see what we will get:

# Create & Check Text
text1 = "I LOVE HONEY".isupper()
text2 = "i LOVE HONEY".isupper()

# Print The Result
print(text1)
print(text2)

The first result will be True since the text contains all upper characters. The second one will return False since the first one is in lower case.

4. Islower() Method

The name for this method also tells what it does. It will check whether all the characters are lower and return True. Otherwise, you will get False if only a character is in the upper case. Let’s see an example:

# Create & Check Text
text1 = "i love honey".islower()
text2 = "I love honey".islower()

# Print The Result
print(text1)
print(text2)

The first text has all the characters in lower case, so it obviously will return True. But the second one has only the first character in the upper case, which will return False.

5. Title() Method

What is the common thing in the titles of the articles? All the first characters of every word should be capitalized. This title() method will convert every first character in the given text to an upper case. Let me show you an example:

# Create a Text
text = "i love honey"

# Change The Text
result = text.title()

# Print The Result
print(result)

The text will be like “I Love Honey” which looks similar to the titles of the articles or any title.

6. Istitle() Method

This istitle() method is obvious what it does after seeing a lot of other methods with similar names. It will verify if all the first latter of your words are capital and return True. Otherwise, it will return False. Let me show you an example:

# Create and Check The Text
text1 = "I Love Honey".istitle()
text2 = "I Love honey".istitle()

# Print The Result
print(text1)
print(text2)

In this example, it will return True for the first text because all the first letters of the words are capitalized. The second example will return False since the last word has a lower letter.

7. Swapcase() Method

This swapcase() method will only change the lower case to the upper case and make the upper case characters into a lower case. Let us see an example:

# Create & Change Text
text1 = "I LoVe HONEY".swapcase()
text2 = "I LOVE HONEY".swapcase()

# Print The Result
print(text1)
print(text2)

The first text contains some words in the upper case, and they will be lower, and other lower characters will be upper. The second one will change from upper to lower since they are upper case characters.

8. Split() Method

This split() method will split your phrase or text based on a particular thing or symbol found in the text. Let me show you an example to understand more:

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

# Split The Text
result = text.split("*")

# Print The Text
print(result)

It will return a python list and give you the word splitter inside that list. We’ve specified that it will split the words whenever it finds the “*” asterisk symbol.

9. Join() Method

From the name, you probably guessed the purpose of this method. It will join (Concatenate) the words and make them one sentence. Let’s see an example of this simple method:

# Create The Words
words = ("I", "Love", "Honey")

# Join The Words
result = "*".join(words)

# Print The Result
print(result)

First, you make a tuple with the words you want to join them. Next, we apply the join() method, and we specify the character as a separator which is the “*” asterisk in this example. When we print the final result, you will get one word separated by the “*” asterisk symbol.

Conclusion

Thanks for reading! We’ve explored a lot of string methods in this series. Still, there are a lot of string methods to explore since the python language is extensive, and you can’t even learn it all if you spend years in this language, but I think all that we explored is a good amount of methods.

Scroll to Top