Table of Contents
There are various Python String Methods used in Python. In this lesson, we will focus on these Python String Methods. We will give examples for each of these Python methods.
Some of the methods used with Python Strings.
Let’s start to learn methods used for python strings one by one.
capitalize() method is used to convert the first character of the string to an upper case. Let’s Show this python string method with an example:
This code will return with the below sentence with upper case first character.
You can practice below!
If we would like to convert all the words in a string to upper case, we use python title() mehod.
As you can see below, all the words’ first character will be converted to upper case as titles.
You can also watch the below video to learn these methods.
You can check also Python List Methods
One of the other Python String Methods related with upper ansd lowers cases is lower() method. We use python upper() method to convert all the characters of the string to lower cases.
Below, with lower() method, we will convert all the cases as lower case in the string.
Like python lower() method, there is an opposite function that converts the string characters to upper case. This is python upper() method. Below, you will see an example that we will convert all the characters of the string as upper characters.
There are other Python String Methods used to check some parts of python strings. With endswith() method, we can control a string if it ends with the specified value.
Below, let’s control our strings if it ends with exclamation mark (!).
This code will return as TRUE because, our string ends with exclamation mark (!). What if we check other character for example dot(.)? Let’s check.
This time, the code will return FALSE, because our string is not ends with dot(.).
isalpha() method is used to check the characters of a string if it is consist of the characters in alphabet. If the whole string is consist of these characters, it returns as True. If not, it returns as False.
Below, the first code will return True, because the string is consist of all alphabetic characters.
Below code will return false, because there are numbers in the string.
Like numbers, if there are space in the string, this python method will return also false.
You can check also Python Set Methods
isnumeric() method of python is used to check if all the characters of the string is numeric characters or not. If all the characters in the string are numeric values, it returns True, if not, it returns False.
Below, you can find three example with isnumeric() function. The first two will return false and the last one will return True. Becasue the whole string in the last example is consist of full numeric values.
split() method is used to list each Word of a string in a list. There are two parameters used with split() function. These are:
Seperator is used to determine where we do split in the string. For example this can be comma (,), dahs(-) etc. Maxsplit is the count of split. These are optional parameters. By default, split() function split each words in the string.
Firsly, let’s use split method without any parameter.
As you can see below, this code will return as a list of strings with all the words in the string.
Now, let’s use comma (,) as seperator.
And now, let’s use also the second parameter as “1”. So, split will be done one time.
partition() method is used to search for a string in the string and divide string by using this given string. It returns with a tuple consist of three elements:
In the below example, we will use “to” as parameter of partitition method to divide our string. The output will be a tuple that has three members.
count() function is used to count the searched string in a string. It return the number of this searched string in the whole string.
Below, we will give an example and count cats string in the whole string.
For the above string, there are three cats Words. So, it will return as 3.
There are also some python string methods used to find and replace any string in a given string. Python replace() function is used to find and replace any string.
With this replace() function, we can use three parameters. The first one is the replaced Word, the second one is the new Word and the last one is the number of change.
The return of this code will be like below. Cats are replaced by dogs.
Let’s use also the third parameter as 2. So, replace will be done only two times.
Below, as a return only first two words will be changed and the last one willl remain. Becasue, our third parameter is 2 that shows the replace count.
python find() method is used to find out any Word in a string. The return of this python fucntion is the index of the find Word if it finds. If it do not find, then it returns as -1.
Let’s show this with two examples. The first one will return with the index of the found string. The second one will return as -1.
There is also other method between python string methods named as index() method. It is similar to find() method but the difference is if index() is not find any value, it returns with an exception, not with -1. If it finds the words in the string, then it returns with index as in find() method.
The output of this code will give error.
Python format() method is used to insert a string to the specified place in the string. This is showed with curly brackets ({}).
Below, we will Show a string that has curly brackets in it. We will fill this area with format.
The output of this code will be like below:
Python strip() method is used to remove the unnecessary spaces arount a string. Below, we will remove the spaces between “dogs” string.
We will add the stripped Word top rint function also. The output will be like below:
If we use a code like below, this time only the sapces at the beginning and end of the string will removed. The spaces between words will remain.
Python join() method is used to combine the string in the string list. We can Show this with the beow examples. As you can see below, in the first example, we will combine all the words together.
In the second example below, we will write a seperator dash(-) between each Word.
In this lesson, we have talked about most used Python String Methods. There are also other methods used for variety reasons. In other lessons, we will also talk about these Python String Methods.
Leave a Reply