The string is a sequence of characters that will give you the word in python. They are used a lot when working with this language. If you are going to process these data, you need to learn their methods which will help you understand more about the text you are working on and extend their functionality. This article is complete for the first part of this series.
1. Format() method
This format() method developed a built-in method inside python to handle complex data formatting. It allows you to insert data inside the string, concatenating many words inside the phrase. Let’s see an example:
# Create The Text
text = "I love {}"
# Use The Format() Method
formatting = text.format("honey")
# Print The Formatting
print(formatting)
You can see we’ve inserted the word “honey” inside the text “I love” in the place where we put the curly brackets, and we use the format() method to fill out this place.
2. Index() Method
This index() method is similar to the find() method we’ve learned in part 1 of this series, and it will give you the index of a particular word inside a phrase. Let’s see an example:
# Create The Text
text = 'I love honey.'
# Get The Word Index
result = text.index("honey")
# Print The Result
print(result)
You will have the same result as the find() method. Instead of getting the -1 in the find() method when the value is not found, the index() will raise a ValueError. You can fix this by using the try/except statements:
# Create The Text
text = 'I love honey.'
try:
# Get The Word Index
result = text.index("hney")
# Print The Result
print(result)
except ValueError:
# Print a Message
print("Word not found")
It will ignore the second part inside the except statement since the word “honey” is found in the text. When the word is not found inside the text, it will raise a ValueError and print that message. The index() method can also accept where to start the search and where it ends:
# Create The Text
text = 'I love honey.'
# Get The Word Index
result = text.index("honey", 0, 10)
# Print The Result
print(result)
Executing this above code will raise a ValueError error since there is no “honey” word in that search range.
3. Isalnum() Method
This method, known as isalnum(), will verify if the given word has only characters or numbers. If the given word has both, it will return True. Otherwise, you will get False. Let’s see an example:
# Create a Text
text1 = "Germany".isalnum()
text2 = "Germany19".isalnum()
text3 = "12345".isalnum()
text4 = "Germany 19".isalnum()
text5 = "G*rmany".isalnum()
# Print The Result
print(text1)
print(text2)
print(text3)
print(text4)
print(text5)
We’ve created many text examples and applied the isalnum() method directly to the text. For the first three texts, it will return True because either they are text, number, or both in the same word.
But, for the other two texts, it will return False since we have space between the word and the number, and also we have the “*” symbol in the last example.
4. Isalpha() Method
This isalpha() method will return True if all the given word is characters. Otherwise, it will return False. Let’s see an example:
# Create a Text
text1 = "Germany".isalpha()
text2 = "Germany19".isalpha()
text3 = "12345".isalpha()
text4 = "Germany Berlin".isalpha()
text5 = "G*rmany".isalpha()
# Print The Result
print(text1)
print(text2)
print(text3)
print(text4)
print(text5)
When you run this code, it will give you the True for only the first word, but other words will return False since they contain space, symbols, or numbers.
5. Isdigit() Method
The name speaks for itself. This isdigit() method will return True if all the string is numbers. Otherwise, will return False if they contain a character or a symbol, or even a space. Let’s see an example:
# Create a Text
text1 = "Germany".isdigit()
text2 = "2022".isdigit()
text3 = "2022 1999".isdigit()
# Print The Result
print(text1)
print(text2)
print(text3)
You will get True as a result for only the second text because it contains only digits. For the other two, it will return False since the first is characters and the last one contain a space.
6. Islower() Method
This islower() method will return True if all the character in the string is lower, not even one. Otherwise, it will return False for one character is capital. Let’s see an example:
# Create a Text
text1 = "Germany".islower()
text2 = "germany".islower()
# Print The Result
print(text1)
print(text2)
The first one will return False since the first character is capital. But it will return True for the second one since they are lower.
7. Isnumeric() Method
This isnumeric() method will return True if all values are 0–9. Otherwise, you will have a False result. Let’s see an example:
# Create a Text
text1 = "-1".isnumeric()
text2 = "2.2".isnumeric()
text3 = "3345".isnumeric()
# Print The Result
print(text1)
print(text2)
print(text3)
For the first two texts, it will return False since the first one have the “-” symbol, which is considered not a numeric value. Also, the second one has the dot, which is not a numeric value.
The last one is all numeric values between 0–9 which will return True. Some values like the exponent will be considered numeric values, and they will also return True.
Conclusion
Thanks for reading! String methods are instrumental when you know how to use them and will be more beneficial if you develop software that will interact with the text, like using artificial intelligence when processing text.