Python String Methods

Python String Methods

When we are dealing with some strings, there are many operations that we might require to do, like converting the string into uppercase or lowercase, capitalizing, getting some substring from the given string, or finding if some string is present in another string, and many more things. These things can be achieved using something called methods. Well, if you are already familiar with the name ‘methods’, it is perfectly fine, but if this word seems new to you, do not worry at all, because we are going to see what methods are. Actually, you may already know what methods are, but you may not know that they are called methods. We will get this clear in a moment.

There is something called functions in python, which is just a block of code that does some particular tasks. We do create functions, because there are a lot of benefits, that we are not going to take here, because it’s a completely different concept, and we are going to go into it later. But the thing is that function basically does something. Like, there can be some function that would add two numbers, or there can be a function that gives the square of some given number, so the function is basically doing some particular tasks.

Python String Methods

We have worked with several functions already, knowingly or unknowingly. We have used some functions like len, print, and input, and these are some of the built-in functions in python, which basically do some particular tasks.

More about functions later, but we need to come back to what are methods basically. So, methods are basically functioning only, but they are inside some particular class, so we are calling them methods. There are different methods in relation to the strings, for us to do different kinds of operations on the strings that we have. Here are some of the methods related to the strings, and we are going to consider some examples related to the methods so that we can understand them in a better way.

Do not even worry about how we are going to use these methods on the strings, and how are they going to work, because we are going to have a look at these methods with help of examples.

For the below table, the string str1 is considered to be as ‘Gyanipandit’

MethodsDescription
capitalize()This method returns the copy of the string, with its first character in uppercase, and the rest in lowercase.
count()This method returns the number of times some specified string occurs in the string.
find()This method searches for some substring and returns the start position of the substring in the string. You have to give a substring as an argument for this method.

Also, this method returns -1 if the substring is not found.

index()This method searches for the substring and returns its position. This method gives an error if the string that it is searching for is not found.
startswith()This method checks if the string starts with the same substring given as an argument.

 Returns True if the string starts with the same substring which was given as an argument.

Else it returns False.
endswith()This method checks if the string ends with the given substring or not.

Returns True if the string ends with the same substring which was given as an argument.

Else it returns False.

isalpha()Returns True if all the characters in the given string are alphabets.

Else, it returns False.

isdigit()Returns True if all the characters in the given string are digits.

Else, it returns False.

islower()Returns True if all the characters in the given string are in lowercase.

Else, it returns False.

isupper()Returns True if all the characters in the given string are in upper case.

Else, it returns False.

isspace()Returns True if all the characters in the given string are in whitespaces.

Else, it returns False.

replace()This method is used to replace some value with another value
upper()This method is used to return a copy of the string with the characters converted to uppercase.
lower()This method is used to return a copy of the string with the characters converted to lowercase.
center()The center method returns a padded string, with the specified characters.
encode()This method returns the encoded version of the given string.
isalnum()This method returns True if all the characters in the string are alphanumeric (either alphabets or numbers). Otherwise, it returns False.

For example, for a string ‘GyaniPandit123’, it would return True.

isdecimal()This method returns True if all the characters in the string are decimal characters. Otherwise, it returns False. For example, for a string ‘29131’, the method would return True.
isnumeric()This method returns True if all the characters in the string are numeric.
join()The join method returns a string, by joining all the elements from an iterable, separating them by the given separator.
ljust()This method returns a string, which is left justified in a string of the specified width. The padding is done using the specified character (default is ASCII space)
lstrip()This method returns a copy of the string, with the leading characters removed. The string argument is given specifying the set of characters to be removed. (It defaults to space)
rstrip()This method returns a copy of the string, with the trailing characters removed. The string argument is given specifying the set of characters to be removed. (It defaults to space)
strip()This method returns a copy of the string, with the leading and the trailing characters removed. The string argument given specifies the set of characters to be removed (defaults to space).
partition()With this method, we can split the string at the first occurrence of the given separator. This method returns a tuple, which contains the part before the separator, the separator itself, and the part after the separator. If the separator is not found, then the tuple is returned with the string, and then there are two empty strings in the tuple.
rfind()This method returns the highest index of the substring (if the substring is found). If the substring is not found, then it returns False.
rsplit()The rsplit method split’s the string from the right at the specified separator, and the list of strings is returned.
split()The split method split’s the string at the given separator, and it returns the list of strings.
splitlines()The splitlines method split’s the string at the line break and returns a list.
title()This method returns the string in the title-cased version, where all the words in the string start with an uppercase character, and the remaining characters are lowercase.
maketrans()This method returns a mapping table for translations, used by the translate method.
translate()This method replaces each character in the string, using the translation table. The translation table can be got using maketrans method.

Now, Let’s see some of the examples, which explain the implementation of the above-discussed methods, so that we can understand these methods in a better way.