String Methods in Python

String Methods in Python

Introduction

A fantastic language is Python. It has an intuitive syntax and is comparatively simple to learn. Python's popularity and success are also attributed to its wide range of libraries.

However, it goes beyond merely the libraries from outside sources. Base Python also offers a wealth of tools and functions to speed up and simplify the common data science tasks.

Types of String Methods

Capitalization

It capitalizes the first letter.

txt = "python is cool!"
txt.capitalize()
'Python is cool!'

Upper

All of the letters are capitalized

txt = "Python is awesome!"
txt.upper()
'PYTHON IS AWESOME!'

Lower

It lowercases all of the letters.

txt = "PYTHON IS AWESOME!"
txt.lower()
'python is awesome!'

Isupper

It verifies that all of the letters are capitalized.

txt = "PYTHON IS AWESOME!"
txt.isupper()
True

Islower

It takes longer to verify that all the letters are lowercase.

txt = "PYTHON IS AWESOME!"
txt.islower()
False<

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.

txt.find("sci")
5

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')

write your code here: Coding Playground

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.