Strings Method

Methods for dynamic coding

A.I HUB
11 min readOct 13, 2024
Image By Author

In the world of Python programming, strings are far more than just sequences of characters they are powerful tools with a wide range of built-in methods that can transform, manipulate and analyze text like never before. Whether you are cleaning data, formatting output or extracting valuable information, Python’s string methods offer endless possibilities to streamline your code and boost efficiency. In this article, we will dive into the essential string methods that will unlock new levels of functionality and control, turning you into a text-handling pro in no time.

Table of Content

  • Strings methods
  • Case changing methods
  • Character classification methods
  • Aligning text within strings
  • Removing unwanted leading and trailing characters
  • Searching and replacing substrings
  • Chaining methods calls

Strings Methods

The str type supports many methods that can be dot suffixed to the name
of the string. We have seen that str is an immutable type, so it does not
provide any methods that change the original string object. All methods that
seem to make changes in the string are designed such that they return a new
modified string object. They do not touch the original string object because
they are not able to, as objects of type str are immutable.
Let us understand this with the help of an example. The method upper() is
used to change the letters in a string to uppercase. We have a string variables.

>>> s = 'Hello'

When we call the method upper on the variable s, it returns a new object
that contains all letters of this string in uppercase.

>>> s.upper()

Output

'HELLO'

The original object to which s was referring remains unchanged. We can
make another variable refer to the object returned by upper.

>>> s1 = s.upper()
>>> s1

Output

'HELLO'

Now, s1 refers to the object returned by the method upper. If we want to
make s refer to this new object, we can write s = s.upper(), then s
will refer to this new object.
So, you cannot change the string object using any method, but you can
assign the new object returned by the method to the string variable referring
to the original string object. str type has lots of methods; we will explore
some of the common ones here. You can try them on the interactive prompt.
To get an up-to-date list of methods, you can call dir(str) or

help(str) on the interactive prompt. To get the description of a particular
method, type help(str.methodname) on the prompt.

Case Changing Methods

This five case-changing methods can be used to perform case

conversions in strings. All of them return a new object, which is a copy of
string s with some changes in the case of the contained letters.

Case-changing methods
>>> s = 'Life is a journey, not a race'
>>> s.lower()

Output

'life is a journey, not a race'
>>> s

Output

'Life is a journey, not a race'

We must assign the returned object to the original variable name to see the
required change.

>>> s = s.title()
>>> s

Output

'Life Is A Journey, Not A Race'

Similarly, while using other methods, if you want to see a change in your
string, you need to reassign it.
When checking for membership or comparing strings, you can ignore the
case by using the upper or lower methods.

>>> 'out' in 'Output'.lower()

Output

True
>>> s = 'telephone'
>>> s[0].upper() in 'AEIOU'

Output

False
>>> response = input('Enter yes or no : ')

Output

Enter yes or no : Yes
>>> response.lower() == 'yes'

Output

True

Character Classification Methods

The methods in this group check the contents of the string, and they return
either True or False. All of them start with 'is’ and their names are self-explanatory.

Character classification methods
>>> s = 'Yes Sir'
>>> s.isalpha()

Output

False
>>> s.isupper()

Output

False
>>> s.istitle()

Output

True

Aligning Text With Strings

This three methods justify a string into a given field size and by

default, the padding is done with spaces.

Text alignment methods

The methods ljust(), rjust() and center() left justify, right
justify or center a string, respectively, such that the string fits within the

number of spaces provided by the argument size. Here, size is the total
length of the string after padding. These methods can be used in printing
tabular data.

>>> s = 'Be a voice, not an echo'
>>> s.ljust(40)

Output

'Be a voice, not an echo
>>> s.rjust(40)

Output

'           Be a voice, not an echo'
>>> s.center(40)

Output

'        Be a voice, not an echo

If size is less than the length of the string, there is no change.

>>> s.center(4)

Output

'Be a voice, not an echo'

You can specify a fill character for padding instead of default spaces.

>>> s.center(40, '*')

Output

'********Be a voice, not an echo*********'

The string is center justified in a field width of 40 and the padding is done
with an asterisk symbol instead of spaces.
The interactive prompt displays the string object returned by a particular
method. As we have seen before, if we want to see the change in the original

string, we need to assign this string object to the original string variable.

Removing Unwanted Leading and Trailing Characters

The str type provides methods to remove leading and trailing whitespaces
or other characters. These methods can be used to sanitize data for further processing. For example, data read from somewhere or input by the user can
be cleaned before storing or processing.

Methods to remove leading and trailing characters

lstrip() and rstrip() remove characters from the left and right sides
of the string, respectively, while strip() removes characters from both
the left and the right sides. The set of characters to be removed is specified
as a string argument. All the characters present in the string argument will be
removed from the left, right or both sides of the string.

>>> '!!..Imagine .. believe .. achieve ..!! ?

Output

'.rstrip('!?. ')
'!!..Imagine .. believe .. achieve'

The argument string is '!?. ' so all exclamation marks, question marks,
full stops and spaces are removed from the right of the string.

>>> '!!..Imagine .. believe .. achieve ..!! ?

Output

'.strip('!?. ')
'Imagine .. believe .. achieve'

Now, the characters are removed from both the left and right of the string, as
we have called strip.

If the argument is omitted or is None, the whitespace characters are
removed.

>>> s = ' All is well '
>>> s.lstrip()

Output

'All is well   '
>>> s.rstrip()

Output

'   All is well'
>>> s.strip()

Output

'ALL IS WELL'

As you know, to see these changes in s, you must reassign s to the new
object.
The methods removeprefix and removesuffix can be used to
remove a prefix or suffix from the string. If the prefix or suffix is not present
then a copy of the original string is returned.

>>> 'PyTorch'.removeprefix('Py')

Output

'Torch'
>>> 'Numpy'.removesuffix('Py')

Output

'Numpy'
>>> 'Numpy'.removesuffix('py')

Output

'Num'

Searching and Replacing Substrings

One of the essential programming tasks is to search your data for specific
information. Python provides many useful methods for searching and
replacing information in a string.

Methods to search a substring

The methods find and index return the index of the first occurrence of
the given substring. If not found, find returns -1 while index raises a
ValueError. The methods rfind and rindex are the same as find
and index except that they search through the string backward from
right to left, so they find the last occurrence of the substring.

Strings methods

In all these methods, you can restrict the search by specifying, optional
arguments start and end as in a slice.
To substitute a substring with another we can use the method replace. It
returns a copy of the string with all occurrences of the first string replaced
with the second string. As usual, the original string remains unchanged and

a new string object is returned. You can restrict the number of replacements
by providing a third argument. That argument represents the number of

occurrences that have to be replaced. Let us use these methods to understand
them better.

>>> s = '''Focus on present, not on past or future
.Focus on yourself, not on others
Focus on the process, not on outcome
Focus on what you can control, not on what you
cannot control'''
>>> s.find('Focus')

Output

0

This call returns the index of the first occurrence of the substring 'Focus'
in the string s. We get 0 here because this substring is present in strings at
the 0th index.

>>> s.rfind('Focus')

Output

111

This method rfind returns the index of the last occurrence of the substring
in the string.

>>> s.find('focus')

Output

-1

We get -1 because 'focus' with f in lowercase is not present in the strings.
The methods index and rindex are similar to find and rfind, but
instead of returning -1, they raise a ValueError if the substring is not
found. In the previous call, if we use the index method, then instead of -1,

we get a ValueError.

>>> s.index('focus')

Output

ValueError: substring not found
>>> s.count('on')

Output

10

The substring 'on' comes 10 times in the strings.

>>> s.startswith('Focus')

Output

True
>>> s.endswith('?')

Output

False

In these methods that we have seen, we can give the start and end index
where the search will be performed.

>>> s.find('Focus', 20, 100)

Output

40

The search is performed in the string portion from index 20 to index 99.
These start and end indexes are interpreted as in slice notation. Similarly, we
can use the start and end indexes in all the other methods of this category.

Now, suppose we have a strings.

>>> s = 'Dev; 22; male; graduate; Bareilly'

We want a string that contains everything after the first occurrence of
semicolon. We can get it by using the index method in the slice notation.

>>> s[s.index(';'):]

Output

'; 22; male; graduate; Bareilly'

s.index(’;’) gives us the index of the first occurrence of the semicolon,

which is 3, and the expression s[s.index(’;’):] gives us a slice from

index 3 till the end. So, we get everything after the first occurrence of the
semicolon. The semicolon itself is included. If we do not want it, we can
specify s.index(’;’)+1 as the start index for the slice.

>>> s[s.index(';')+1:]

Output

' 22; male; graduate; Bareilly'

Now, we assign this slice object to the name s2.

>>> s2 = s[s.index(';')+1:]
>>> s2

Output

' 22; male; graduate; Bareilly'

So, s2 is a string that contains everything after the first occurrence of the
semicolon. Now, instead of index, let us write rindex.

>>> s2 = s[s.rindex(';')+1:]
>>> s2

Output

' Bareilly'

Now we get a string that contains everything after the last occurrence of
semicolon. Let us combine both index and rindex in this slice.

>>> s2 = s[s.index(';')+1: s.rindex(';')]
>>> s2

Output

' 22; male; graduate'

This gives us everything between the first and last occurrence of the

semicolon. We could have also used the find() method here, but it is
better to use the index() method in these types of cases, as find returns -1
if the substring is not found, and -1 is a valid index value in Python. We
might get incorrect results if we use find(). Let us understand this with
the help of an example. Suppose we want everything from the beginning of

the string s to the first occurrence of the substring xy.

>>> s2 = s[: s.index('xy')]

Output

ValueError: substring not found

The substring 'xy' is not present in s, so we get this error. Now, instead of
index, let us use find and see.

>>> s2 = s[: s.find('xy')]

This does not give any error. Let us see what is s2.

>>> s2

Output

'Dev; 22; male; graduate; Bareill'

The find method returned -1 since the substring was not present. So, this
slice represents the whole string from starting to index -2.

Now, let us try the replace method. Again, we take this multiline strings.

>>> s = '''Focus on present, not on past or future
.Focus on yourself, not on others
Focus on the process, not on outcome
Focus on what you can control, not on what you can't control'''
>>> s2 = s.replace('Focus', 'Concentrate')
>>> print(s2)

Output

Concentrate on present, not on past or future
Concentrate on yourself, not on others
Concentrate on the process, not on outcome
Concentrate on what you can control, not on what you can't control

All the occurrences of 'Focus' are replaced with 'Concentrate’.

>>> s2 = s.replace('Focus', 'Concentrate', 3)
>>> print(s2)

Output

Concentrate on present, not on past or future
Concentrate on yourself, not on others
Concentrate on the process, not on outcome
Focus on what you can control, not on what you can't control

Now, only the first three occurrences are replaced. By replacing it with an
empty string, we can delete characters from the string.

>>> s2 = s.replace('not', '')
>>> print(s2)

Output

Focus on present, on past or future
Focus on yourself, on others
Focus on the process, on outcome Focus on what you can control, on what you can't control

All occurrences of substring 'not' were removed. As a result of removal,
we get double spaces in many places. We want only one space in those
places. For this, we can replace double spaces with a single space by making
one more call to replace method.

>>> s2 = s.replace('not', '').replace(' ', ' ')
>>> print(s2)

Output

Focus on present, on past or future
Focus on yourself, on others
Focus on the process, on outcome
Focus on what you can control, on what you can't control

This chained call works because the replace method returns a string
object.

Chaining Methods Calls

Most string methods return a string object, so you can apply multiple
methods to a string to get the desired result. We saw this while using the
rstrip method and the replace method.

>>> s = ' hello '
>>> s = s.strip().upper().center(20, '*')
>>> s

Output

'*******HELLO********'

The methods are executed from left to right, one at a time. In this example,
the method strip is called on the strings, then the method upper is
called on the string returned by strip and the method center is called on
the string returned by the method upper. The string object returned by
center is assigned to s. The order of the methods matter the output might

change if the order is changed.

>>> s = ' hello
>>> s = s.center(20, '*').upper().strip()
>>> s

Output

'*** HELLO ****'

Conclusion

String methods in Python are the powerhouse tools that turn basic text manipulation into a seamless, efficient process. From formatting and searching to transforming and validating strings, these methods unlock a new level of control over text data. Whether you are cleaning up datasets, building user-friendly applications or handling complex text processing tasks, mastering these methods gives you the edge. By utilizing the full range of Python’s string capabilities, you are not just working with data, you are shaping it, optimizing your code and solving problems with elegance and precision.

--

--

A.I HUB
A.I HUB

Written by A.I HUB

A.I HUB is a learning platform, where you can learn from all sorts of courses for free we help individuals and youngster by providing quality content.

No responses yet