<Since the next three methods are comparable, I'll give examples that use each one.
Alphabetic
It verifies that every character is a number.
Isalpha
It verifies that the alphabet contains all of the characters.
Isalnum
It verifies that every character is an alphanumeric (i.e. letter or number).
# Example 1 txt = "Python" print(txt.isnumeric()) False print(txt.isalpha()) True print(txt.isalnum()) True # Example 2 txt = "2021" print(txt.isnumeric()) True print(txt.isalpha()) False print(txt.isalnum()) True # Example 3 txt = "Python2021" print(txt.isnumeric()) False print(txt.isalpha()) False print(txt.isalnum()) True # Example 4 txt = "Python-2021" print(txt.isnumeric()) False print(txt.isalpha()) False print(txt.isalnum()) False |
Count
It keeps track of how frequently a certain character appears in a string.
txt = "Data science" txt.count("e") 2 |
Find
It provides the index of the first time the specified character appears in a string.
txt = "Data science" txt.find("a") 1 |
We can also track down a character's subsequent appearances.
txt = "Data science" txt.find("a") 1 |
The find method returns the index where a sequence of characters begins if we pass a sequence of characters.
Startswith
It determines whether a string begins with the specified character. This approach can be used as a filter in list comprehension.
mylist = ["John", "Jane", "Emily", "Jack", "Ashley"] j_list = [name for name in mylist if name.startswith("J")] j_list ['John', 'Jane', 'Jack']
|
Endswith
It determines if a string has the specified character at the end.
txt = "Python" txt.endswith("n") True |
Case distinctions apply to both the endswith and startswith approaches.
txt = "Python" txt.startswith("p") False txt.startswith("P") True |
Replace
It substitutes the specified set of characters for a string or a portion of a string.
txt = "Python is awesome!" txt = txt.replace("Python", "Data science") txt 'Data science is awesome!' |
Split
A list containing each half is returned after splitting a string at the points where the requested character appears.
txt = 'Data science is awesome!' txt.split() ['Data', 'science', 'is', 'awesome!'] |
Partition
It divides a string into three pieces and then produces a tuple with those parts in it.
txt = "Python is awesome!" txt.partition("is") ('Python ', 'is', ' awesome!') txt = "Python is awesome and it is easy to learn." txt.partition("and") ('Python is awesome ', 'and', ' it is easy to learn.') |
Exact three portions are returned by the partition procedure. If a character is used for partitioning more than once, only the first instance is taken into consideration.
txt = "Python and data science and machine learning" txt.partition("and") ('Python ', 'and', ' data science and machine learning') |
Conclusion
We frequently work with textual data when undertaking data science. Additionally, compared to plain integers, textual data requires significantly more preprocessing. Thankfully, the built-in string functions in Python can handle such tasks quickly and effectively.