AI

What Are Python String Methods?

Python String Methods

Introduction

Python provides a rich set of built-in string methods that allow developers to manipulate strings easily and efficiently. String methods are functions that can be called on a string object and operate on its contents, modifying or returning a new version of the string.

Some of the most commonly used string methods in Python include len(), which returns the length of the string, strip(), which removes whitespace characters from the beginning and end of a string, lower() and upper(), which return a new string with all characters in lowercase or uppercase, respectively, and replace(), which replaces occurrences of a specified substring with a new substring. Other useful methods include split(), which splits a string into a list of substrings based on a delimiter, and join(), which concatenates a list of strings into a single string using a specified delimiter.

These methods provide a way to perform various operations on strings such as formatting, searching, replacing, and more. In this article, we will cover various string methods available in Python and their applications.

Also Read: How Long Does It Take To Learn Python

What are Python String Methods

Python strings are just Unicode characters enclosed in quotes. In any programming language, provisioning functions to manipulate the basic types is absolutely necessary to lower the development overhead. It cuts out the risk of non-optimal implementations for the most basic units of code.

Python String Methods

Understanding the list of Python String Methods

Source: YouTube

Python String capitalize()

This method allows us to capitalize the first character of the string.

lower_case = "subscribe to ai plus info." 
upper_case = lower_case.capitalize() 
print(upper_case) 
# Subscribe to ai plus info.

Python String casefold()

This method allows us to convert all the characters to lowercase. It is similar to lower() method but is more aggressive in the conversion, it figures out other characters as well, for example converting ‘ß’ to ‘ss’.

txt = "SOmethingG" 
lower_case = txt.capitalize() 
print(lower_case) 
# something

Python String center()

The center() method returns a string centered in a specified width.

txt = "Shift" 
shifted_txt = txt.center(5) 
print(shifted_txt) 
#    Shift

The second parameter is used to specify which character shall be used to fill up the empty space. By default, it is an empty space.

txt = "Shift" 
shifted_txt = txt.center(5,'0') 
print(shifted_txt) 
# 00000Shift00000

Python String count()

count() in pythonallows us to count the number of occurrences of a string in another.

txt = "This sting is shifted once, So it is now shifted" 
cnt = txt.count('shifted') 
print(cnt) 
# 2

We can configure the start and end index for counting the string. By default, the count is applied on the whole string.

txt = "This sting is shifted once, So it is now shifted" 
cnt = txt.count('shifted',0,15) 
print(cnt) 
# 1

Python String encode()

UTF-8 encode a string

txt = "AIPlusInfo" 
encoding = txt.encode()
# print the encoding.

The parameter of the encode() function allows us to specify the error method. Below are the list of valid error coding.

'backslashreplace'– uses a backslash instead of the character that could not be encoded
'ignore'– ignores the characters that cannot be encoded
'namereplace'– replaces the character with a text explaining the character
'strict'– Default, raises an error on failure
'replace'– replaces the character with a question mark
'xmlcharrefreplace'– replaces the character with an XML character

Python String endswith()

Check if the string ends with a particular string and ends with a boolean denoting this.

txt = "AIPlusInfo" 
print(txt.endswith("Info"))

# True

Python String expandtabs()

The expandtabs() method returns a copy of the string with all tab characters ‘\t’ replaced with whitespace characters until the next multiple of tab size parameter.

txt = "H\te\ty" 
expanded = txt.expandtabs(2)
print(expanded)

# H e y

Python String find()

find()in Python is used to search for a specified substring within a string. It returns the index of the first occurrence of the substring, or -1 if the substring is not found. The syntax of the find() method is as follows:

txt = "Something about this function is Something" 
f = txt.find('Some')
print(f)

# 0

Where substring is the string that you want to search for, start is the starting index (optional, default is 0), and end is the ending index (optional, default is the end of the string).

txt = "Something about this function is Something" 
f = txt.find('Some', 10)
print(f)

# 33

Python String format()

format() in Python is used to format specified values inside a string. It allows you to substitute placeholders in the string with specified values. The syntax of the format() method is as follows.

Where value1, value2, ... are the values that you want to substitute in the string. The placeholders in the string can be represented as curly braces {} and can contain optional format specifications, for example:

txt = "This name is {} {}"
print(txt.format("Lord", "Henry"))

# This name is Lord Henry

Python String format_map()

format_map()in Python is used to format a string using the mapping provided in a dictionary or any other mapping object. The mapping object should have keys that match the placeholder names in the format string.

nameMap = {'name':'Henry','title':'Lord'}
formattedString = '{title} {name} is here'.format_map(nameMap)

print(formattedString)

# Lord Henry is here.

Python String index()

index()in Python is used to find the first occurrence of a substring in a string. It returns the index of the substring in the string.

If the substring is not found, it raises a ValueError exception.

txt = "What are you up to?"
idx = txt.find("are")

print(idx)
# 5

Python String isalnum()

isalnum()in Python returns True if all the characters in a string are alphanumeric (i.e., letters and digits), and False otherwise.

txt = "something12"
print(txt.isalum())

# True

Python String isalpha()

isalpha()in Python returns True if all the characters in a string are letters, and False otherwise.

txt = "something12"
print(txt.isalpha())

# False

txt = "something"
print(txt.isalpha())

#True

Python String isdecimal()

isdecimal()in Python returns True if all the characters in a string are decimal characters (i.e., digits 0 to 9), and False otherwise.

txt = "123"
print(txt.isdecimal())

# True

Python String isdigit()

isdigit()in Python returns True if all the characters in a string are digits, and False otherwise.

txt = "42"
print(txt.isdigit())

# True

Python String isidentifier()

isidentifier()in Python returns True if a string is a valid identifier, and False otherwise. A string is considered a valid identifier if it starts with a letter or underscore, followed by any number of letters, digits, or underscores.

txt = "machineLearning"
print(txt.isidentifier())

# True

Python String islower()

  1. islower()in Python returns True if all the characters in a string are in lowercase, and False otherwise.
txt = "machinelearning"
print(txt.islower())

# True

Python String isnumeric()

isnumeric() in Python returns True if all the characters in a string are numeric characters, and False otherwise.

txt = "42"
print(txt.isnumeric())

# True

Python String isprintable()

isprintable()in Python returns True if all the characters in a string are printable, and False otherwise. Non-printable characters are characters that are not visible on the screen, such as null, backspace, and bell.

txt = "What are you up to?"
print(txt.isprintable())

# True

Python String isspace()

isspace()in Python returns True if all the characters in a string are whitespace characters, and False otherwise.

txt = "   "
print(txt.isspace())

# True

Python String istitle()

istitle()in Python returns True if the first character of each word in a string is capitalized, and False otherwise.

txt = "Hello World"
print(txt.istitle())
# True

txt = "hello World"
print(txt.istitle())
#False

Python String isupper()

isupper()in Python returns True if all the characters in a string are in uppercase, and False otherwise.

txt = "HELLO WORLD"
print(txt.isupper())

# True
# It only includes the actual charaters

Python String join()

join()in Python is used to join a list of strings into a single string, with the separator specified as the argument to the method.

list = ['cake', 'biscuits', 'cookies']
combines = " ".join(list)

print(combines)
# 'cake biscuits cookies'

Python String ljust()

ljust()in Python is used to left-justify a string within a specified width. The method takes the width as an argument and returns a string with the original string padded with spaces on the right to meet the specified width.

txt = 'Hey Ai'
ljustified  = txt.ljust(20) + 'right end'

print(ljustified)

# 'HEY AI               right end'

Python String lower()

lower()in Python is used to convert all the characters in a string to lowercase.

txt = 'Hey Ai'
print(txt.lower())

# 'hey ai'

Python String lstrip()

lstrip()in Python is used to remove all leading whitespaces from a string.

txt = 'Hey Ai'
print(txt.lstirp())

# 'hey ai'

Python String maketrans()

maketrans()in Python is used to create a one-to-one mapping of characters in a string to replace them with other characters. This method is typically used with the translate() method.

txt = "Hey Tony!"
x = "onTy"
y = "amSi"

mytable = str.maketrans(x, y)
print(txt.translate(mytable))

# 'Hei Sami!'

Python String partition()

partition()in Python is used to split a string into three parts: the part before the specified separator, the separator itself, and the part after the separator.

txt = "Hello world"
print(txt.partition('w'))

# ['Hello ', 'w', 'orld']

Python String replace()

replace()in Python is used to replace a specified substring with another string.

txt = "Hello world"
print(txt.replace('l','LL'))

# HeLLLLo worLLd

Python String rfind()

rfind()in Python is used to search for a specified substring in a string and return the index of the last occurrence of the substring. If the substring is not found, the method returns -1.

txt = "Hello world"
print(txt.rfind('world'))

# 6

Python String rindex()

rindex()in Python is used to search for a specified substring in a string and return the index of the last occurrence of the substring. If the substring is not found, the method raises a ValueError exception.

It is pretty similar to rfind() with the difference of throwing an exception instead of returning -1 when the substring is not found.

txt = "Hello world"
print(txt.rindex('world'))

# 6

Python String rjust()

rjust()in Python is used to right-justify a string within a specified width. The method takes the width as an argument and returns a string with the original string padded with spaces on the left to meet the specified width.

txt = "Something"
print('leftEnd' + txt.rjust(15))

# 'leftEnd       Something'

Python String rpartition()

rpartition() in Python is used to split a string into three parts: the part before the last occurrence of a specified separator, the separator itself, and the part after the separator.

The position of the split is the right most occurrence for the desired substring

txt = "Something is some of a something"
print(txt.rpartition('some'))

# ['Something is some of a ', 'some', 'thing']

Python String rsplit()

rsplit() in Python is used to split a string into a list of strings, with the separator specified as an argument.

txt = "Something is some of a something"
print(txt.rsplit('some'))

# ['A ', 'thing is ', ' of a ', 'thing']

Python String rstrip()

rstrip()in Python is used to remove all trailing whitespace from a string.

txt = "Something    "
print(txt.rstrip())

# "Something"

Python String split()

split()in Python is used to split a string into a list of strings, with the separator specified as an argument.

txt = "I am reading an article"
print(txt.split())

# ["I", "am", "reading", "an", "article"]

Python String splitlines()

splitlines()in Python is used to split a string into a list of strings, with each string representing a line in the original string.

txt = "I am \n tired"
print(txt.splitlines())

# ['I am','tired']

Python String startswith()

startswith()in Python is used to check if a string starts with a specified substring. The method returns True if the string starts with the specified substring and False otherwise.

txt = "Ai Plus Info"
print(txt.startswith('ai'))

# True

Python String strip()

strip()in Python is used to remove all leading and trailing whitespace from a string.

txt = "    Something      "
print(txt.strip())

# 'Something'

Python String swapcase()

swapcase()in Python is used to convert all uppercase characters in a string to lowercase and all lowercase characters to uppercase.

txt = "AiPlusInfo"
print(txt.swapcase())

# aIpLUSiNFO

Python String title()

title()in Python is used to convert the first character of each word in a string to uppercase and the rest of the characters to lowercase.

txt = "what are you up tO?"
print(txt.title())

# 'What Are You Up To?'

Python String translate()

translate()in Python is used to replace specified characters in a string with other characters, using a mapping created with the maketrans() method.

txt = "Hey Tony!"
x = "onTy"
y = "amSi"

mytable = str.maketrans(x, y)
print(txt.translate(mytable))

# 'Hei Sami!'

Python String upper()

upper()in Python is used to convert all characters in a string to uppercase.

txt = "Hey Tony!"
print(txt.upper())

# 'HEY TONY!'

Python String zfill()

zfill()in Python is used to left-justify a string within a specified width, by adding zeros (0) to the left of the string until it meets the specified width.

txt = "Hey Tony!"

print(txt.zfill(3))

# '000Hey Tony!'

Also Read: Python Argmax

Conclusion

The above list is of course not exhaustive and there are multiple packages out there that help support a bunch of operations like formatting, searching, and splitting strings. These come in pretty handy during data analysis, automation, web development etc.

A few examples of applications of Python string functions are:

  1. Text processing and manipulation: Python string functions can be used to manipulate text in many ways. For example, the replace() function can be used to replace one string with another within a larger text string, while the split() function can be used to split a text string into separate words or phrases. These functions are commonly used in text processing applications such as natural language processing and machine learning.
  2. Data cleaning and transformation: In data analysis and data science, Python string functions can be used to clean and transform data before analysis. For example, the strip() function can be used to remove whitespace from the beginning and end of a string, while the join() function can be used to concatenate a list of strings into a single string. These functions are commonly used in data cleaning and transformation tasks, such as preparing data for analysis or visualization.
  3. Regular expressions: Python provides a powerful regular expression module called re, which is used to match and manipulate patterns within strings. Regular expressions are a powerful tool for text processing and data cleaning, and can be used to extract specific pieces of information from text strings. The re module provides many functions and methods for working with regular expressions, including match(), search(), and findall().
  4. Web development: Python string functions can be used in web development to generate HTML and other types of markup. For example, the format() function can be used to dynamically insert values into an HTML template, while the encode() and decode() functions can be used to encode and decode URLs and other types of web data. These functions are commonly used in web applications to generate dynamic content or to process user input.
  5. File input/output: Python string functions can be used to read and write text files, including CSV and other types of structured data files. For example, the open() function can be used to open a file for reading or writing, while the read() and write() functions can be used to read and write text to and from the file. These functions are commonly used in data processing and analysis tasks, as well as in web development.

In conclusion, Python string functions are a powerful tool for working with and manipulating strings, and they have a wide range of applications in different fields such as text processing, data cleaning, regular expressions, web development, and file input/output. The Python programming language’s built-in string functions, combined with its extensive library ecosystem, make it a popular choice for a wide range of applications.

Also Read: What is a Sparse Matrix? How is it Used in Machine Learning?

References

Python String Methods. https://www.w3schools.com/python/python_ref_string.asp. Accessed 21 Feb. 2023.

“—.” GeeksforGeeks, 21 July 2021, https://www.geeksforgeeks.org/python-string-methods/. Accessed 21 Feb. 2023.

Yıldırım, Soner. “15 Must-Know Python String Methods.” Towards Data Science, 29 Oct. 2021, https://towardsdatascience.com/15-must-know-python-string-methods-64a4f554941b. Accessed 21 Feb. 2023.