Python String Methods

python-string-methods-ipcisco

What Are The Python String Methods?

 

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. Let’s start to learn methods used for python strings one by one.

 


 

Python String Methods : capitalize() Method

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:

 

msg = "welcome to python course!"
x = msg.capitalize()
print (x)

 

This code will return with the below sentence with upper case first character.

 

Welcome to python course!

 

python-string-methods-capitalize-method

 


 

Python String Methods : title() Method

If we would like to convert all the words in a string to upper case, we use python title() mehod.

 

msg = "welcome to python course!"
x = msg.title()
print (x)

 

As you can see below, all the words’ first character will be converted to upper case as titles.

 

Welcome To Python Course!

 

python-string-methods-title-method

 


 

Python String Methods: lower() Method

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.

 

msg = "weLcoMe to PytHon CouRse!"
x = msg.lower()
print (x)

 

welcome to python course!

 

python-string-methods-lower-method

 


 

upper() Method

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.

 

msg = "weLcoMe to PytHon CouRse!"
x = msg.upper()
print (x)

 

WELCOME TO PYTHON COURSE!

python-string-methods-upper-method

 


 

endswith()

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 (!).

 

msg = "weLcoMe to PytHon CouRse!"
x = msg.endswith("!")
print (x)

 

true

 

python-string-methods-endswith-method

 

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.

 

msg = "weLcoMe to PytHon CouRse!"
x = msg.endswith(".")
print (x)

 

This time, the code will return FALSE, because our string is not ends with dot(.).

 

false

 


 

isalpha() Method

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.

 

msg = "welcome"
x = msg.isalpha()
print (x)

 

true

 

python-string-methods-isalpha-method

 

Below code will return false, because there are numbers in the string.

 

msg = "welcome123"
x = msg.isalpha()
print (x)

 

false

 

Like numbers, if there are space in the string, this python method will return also false.

 

msg = "welcome to python"
x = msg.isalpha()
print (x)

 

false

 


 

isnumeric() Method

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.

 

msg = "welcome"
x = msg.isnumeric()
print (x)

 

false

 

python-string-methods-isnumeric-method

 

msg = "welcome123"
x = msg.isnumeric()
print (x)

 

false

 

msg = "123"
x = msg.isnumeric()
print (x)

 

true

 


 

split() Method

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
  • maxsplit

 

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.

 

msg= "welcome to python course"
x = msg.split()
print(x)

 

As you can see below, this code will return as a list of strings with all the words in the string.

 

['welcome', 'to', 'python', 'course']

 

python-string-methods-split-method

 

Now, let’s use comma (,) as seperator.

 

msg= "welcome to python course,how are you,how is it going"
x = msg.split(",")
print(x)

 

['welcome to python course', 'how are you', 'how is it going']

 

And now, let’s use also the second parameter as “1”. So, split will be done one time.

 

msg= "welcome to python course,how are you,how is it going"
x = msg.split(",",1)
print(x)

 

['welcome to python course', 'how are you,how is it going']

 


 

partition() Method

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:

 

  • string before match
  • matched string
  • string after match

 

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.

 

msg = "welcome to python course"
x = msg.partition("to")
print(x)

 

('welcome ', 'to', ' python course')

 

python-string-methods-partition-method

 


 

Python String Methods: count() Method

 

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.

 

msg = "I like cats. Because cats are cute. I think cats can live in every house."
x = msg.count("cats")
print(x)

 

For the above string, there are three cats Words. So, it will return as 3.

 

3

 

python-count-method-ipcisco-python-course

 


Python String Methods: replace() Method

 

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.

 

msg = "I like cats. cats are cute."
x = msg.replace("cats", "dogs")
print(x)

 

The return of this code will be like below. Cats are replaced by dogs.

 

I like dogs. dogs are cute.

 

python-replace-method-ipcisco-python-course

 

Let’s use also the third parameter as 2. So, replace will be done only two times.

 

msg = "I like cats. cats are cute. cats are real friends."
x = msg.replace("cats", "dogs", 2)
 print(x)

 

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.

 

I like dogs. dogs are cute. cats are real friends.

 


 

find() Method

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.

 

msg = "I like cats."
x = msg.find("cats")
print(x)

 

7

 

python-find-method-ipcisco-python-course

 

msg = "I like cats."
x = msg.find("dogs")
print(x)

 

 

 


 

index() Method

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.

 

msg = "Welcome to python course!"
x = msg.index("python")
print(x)

 

11

 

python-index-method-ipcisco-python-course

 

msg = "Welcome to python course!"
x = msg.index("java")
print(x)

 

The output of this code will give error.

 


 

format() Method

 

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.

 

msg = "Legolas is a {} character."
print(msg.format("Elf"))

 

The output of this code will be like below:

 

Legolas is a Elf character.

 

python-format-method-ipcisco-python-course

 


 

strip() Method

 

Python strip() method is used to remove the unnecessary spaces arount a string. Below, we will remove the spaces between “dogs” string.

 

msg = "     Elf       "
x = msg.strip()
print("Legolas is an", x)

 

We will add the stripped Word top rint function also. The output  will be like below:

 

Legolas is an Elf

 

python-strip-method-ipcisco-python-course

 

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.

 

msg = " legolas    aragorn   gimli  "
x = msg.strip()
print(x)

 

legolas    aragorn   gimli

 


 

join() Method

 

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.

 

characters = ["Aragorn", "Arwen", "Legolas", "Gimli", "Frodo"]
print ("".join(characters))

 

AragornArwenLegolasGimliFrodo

 

In the second example below, we will write a seperator dash(-) between each Word.

 

characters = ["Aragorn", "Arwen", "Legolas", "Gimli", "Frodo"]
print ("-".join(characters))

 

Aragorn-Arwen-Legolas-Gimli-Frodo

 

python-string-join-method-ipcisco-1

 

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.

 

Back to: Python Programming Course > Python Strings

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact

  • info@ipcisco.com
IPCisco is the Winner! “Best Certification Study Journey of 2019!”

Cisco-ITBlogAwards-2019-Winner-IPCisco-k