Programs

Top 10 Python String Methods[With Examples]

What are Python Strings

In Python, strings are an immutable sequence of Unicode characters encased in one, two, or three quotes and are used for encoding. The primary purpose of Unicode is to encode every single character in all languages in a uniform manner.

Computers understand only binary numbers, and the characters that appear on the screen are internally interpreted and stored as binary numbers (combination of 0 and 1). The process of converting a character into a number is called encoding, and the reverse process (number to character) is termed decoding. 

How are strings created in Python?

Strings are created by writing the characters within single or double quotes. Three quotes can also be used, but they are generally used only to denote docstrings and multiline strings. 

Here’s a program to demonstrate how we define strings in Python

my_string = ‘Welcome’

print(my_string)

my_string = “Welcome”

print(my_string)

my_string = ”’Welcome”’

print(my_string)

# we can use triple quotes to include multiple lines of strings

my_string = “””Welcome to

       my world”””

print(my_string)

The above program will produce the following output:

Welcome

Welcome

Welcome

Welcome to my world

If a text like “she asks,” how are you?” needs to be printed as such, it will result in Syntax Error as the statement has both single and double-quotes. In this case, triple quotes should be used.

In place of triple quotes, escape sequences can also be used. An escape sequence begins with a backlash. If a string is represented with a single quote, all single quotes within a string should be escaped. The same is the case with double quotes too. 

Below is a program that explains how escaping works. 

print(”’She said, “What is going on?””’)

print(‘She said, “What\ is going on?”‘)

print(“She said, \”What is going on?\””)

Output:

She said, “What is going on?”

Raw strings can also be used for ignoring escape sequences inside of a string. This can be done by using r or R at the beginning of the string.

Here’s looking at some of the functions you can perform on Python strings without using Python Methods.

1. Accessing Individual Characters in a String

Individual characters can be accessed through indexing and a set of characters with the help of slicing. There are two types of errors that can arise while accessing string characters: 

  • The range of the index starts from 0. Trying to access a character beyond the index range will result in IndexError.
  • The index can only be an integer. Involving float numbers or other data types will result in TypeError.

Negative indexing can be used for sequences in Python. The index -1 denotes the last item, -2 represents the penultimate item, and so on. A range of items in a string can be accessed with the help of a slicing operator, colon (:). The index is considered to be between the elements for splicing. 

Here is a program for accessing string characters in Python:

str = ‘Character’

print(‘str = ‘, str)

#1st character

print(‘str[0] = ‘, str[0])

#End character

print(‘str[-1] = ‘, str[-1])

#2nd to 5th character will be sliced

print(‘str[1:5] = ‘, str[1:5])

#6th to 2nd last character will be sliced

print(‘str[5:-2] = ‘, str[5:-2])

The output of the above program will be:

str = Character

str [0] = c

str [-1] = r

str [1:5] = hara

str[5:-2] = ct

2. Deleting a String

Strings are immutable, and their elements cannot be altered once they are declared or assigned. Different strings can only be reassigned to the same name. 

The characters can neither be deleted nor removed from a string. However, an entire string can be deleted with the help of the del keyword.

3. Merging strings

The process of merging or combining two or more strings to make a single string is called concatenation. The operator + is used for concatenation in Python. The simplest way of concatenation is by writing two strings together. The operator * is used to repeat the string a specified number of times. 

Below is a program that explains how strings are merged.

str1 = ‘Hi’

str2 =’Hello’

print(‘str1 + str2 = ‘, str1 + str2)

print(‘str1 * 3 =’, str1 * 3)

Output:

str1 + str2 = HiHello

str1 * 3 = HiHiHI

Python String Methods: How are they used?

Python has a load of in-built methods that can be used on strings. Let us look at the different Python string methods and understand how they are used: 

1. Python String Capitalize()

This string method will return a copy of the string after capitalizing its first character. The rest of the characters will remain lower case. 

Below is an example of how capitalize() works:

>>> mystring = “hi there”

>>> print(mystring.capitalize())

Output:

Hi there

2. Python String Casefold()

Casefold returns a case folded version of the string. These case folded strings can be utilized for caseless matching.

Here’s how this works:

>>> mystring = “hi THERE”

>>> print(mystring.casefold())

Output:

hi there

3. Python String Center(width,[fillchar]

This Python string method is used for centering the string. The fillchar is to pass the argument specifying the padding or fill character for the strings. The original string will be returned if the width is lesser than required.

Here’s is program showing how this works:

mystring = ‘Centered’

x = mystring.center(14, ‘-‘)

print(x)

Output:

—Centered—

4. Python String Count(sub, [start],[end])

This Python string method prints the number of substring occurrences in the range (beginning to the end) that do not overlap. Optional arguments like start and end are represented in slice notations.

The syntax for the Count method is:

string.count(substring,start=…,end=…)

The string count() method needs only one parameter for execution, but it can also have two additional optional parameters:

  • Python substring: It represents the string whose count is to be determined.
  • start (optional): The beginning index is encased in the string where the search begins.
  • end (optional): The final index within the string where the search stops. 

5. Python String Encode()

This Python string method displays the encoded copy of the string as a bytes object. The default encoding used by Python is utf-8. The syntax for encode() method is written as follows.

string.encode(encoding=’UTF-8′, errors=’strict’)

The encode () string does not require any default parameter. It returns the utf-8 encoded copy of the string. If the encoding fails, it shows UnicodeDecodeError. There are two parameters in the encode()method:

  • encoding: It displays the string that must be encoded.
  • errors: This is the response if there is an encoding failure. There are a total of six responses given when encoding fails: strict, ignore, replace, xmlcharrefreplace, backslashreplace, and namereplace. 

6. Python String Endswith()

This method displays “true” when a string ends with the mentioned suffix. If not, it displays “false”. 

The syntax for endswith() method is:

str.endswith(suffix[, start[, end]])

The endswith() string method has three parameters.

  • suffix: This denotes a single string or a set of suffixes to be checked. 
  • start: This is an optional parameter that denotes the initial position where the suffix must be checked within a string.
  • end: This is another optional parameter that denotes the ending place where the suffix must be checked within a string.

7. Python String expandtabs()

The string expandtabs() will return a string copy that has all the tab characters replaced by whitespace characters. The syntax of expand tabs() is:

string.expandtabs(tabsize )

The expand tabs() always has an integer tab size argument. The default tab size used by this string method is 8. The string returned by expand tab() has all the ‘ \t’ characters replaced by whitespace till the next multiple of tab size parameter.

8. Python String find()

The find() method will return the index of the first substring occurrence. If the substring occurrence is not found, it returns -1. The syntax of the find() method is:

str.find(sub[, start[, end]])

The find() method makes use of three parameters.

  • sub: It denotes the substring to be found in the str string.
  • start: This is an optional parameter that denotes the initial position where the string is to be searched.
  • end: This is another optional parameter that denotes the ending position where the string is to be searched. 

9. Python String format()

This Python string method is used to format the given string to obtain a better-looking output.

The syntax of the format() method is:

template.format ( p0,p1,…., k0=v0, k1=v1, …)

  • Here p0 and p1 are the positional arguments.
  • k0, k1,.. are keyword arguments.
  • v0, v1 is the value of the keyword arguments.

This Python string method can have any number of parameters, but they are classified under two categories:

  • Positional parameters: A set of parameters that are accessible through an index of parameters enclosed within curly braces {}.
  • Keyword parameters: A set of parameters that are accessible through a key parameter enclosed within curly braces {}. 

10. Python String isalpha()

This method displays True when all the characters inside the string are alphabets. If they are not alphabets, the method displays False. The syntax of the isalpha() method is:

string. isalpha ()

The isalpha() method does not use any parameter, and the two return values of the method will either be True or False.

String operations can be coupled with many other operations, making it the most popular data type in Python. If you’re looking to deep-dive into Python and learn in detail about this highly sought-after data science skill, you should join upGrad and IIIT Bangalore’s Executive PG Program in Data Science for a valuable learning experience. 

The 12-month course promises 400+ content hours with 25 expert coaching sessions and 20+ live learning sessions. The platform’s 40,000+ learner base empowers students with the knowledge and expertise to tackle data science applications on the global level. So, don’t wait, enroll today! 

How many types of strings does Python support?

Python supports only two types of strings. They are:
1. Single line strings
2. Multi-Line strings.

What is the function of type() in Python?

The type() returns the data type of the input object specified in the form of an argument. The function is crucial for debugging.

What is a string data type?

A string data type is a set of characters that can be literal constants or variables. The variables might permit the mutation of their elements and altering their lengths while the constants don't.

Plan Your Data Science Career Today

0 replies on “Top 10 Python String Methods[With Examples]”

Accelerate Your Career with upGrad

Our Best Data Science Courses

×