Python String Contains    

python-string-contains

In Python Coding, find and replace functions in strings are very popular. In many codes, coders uses these functions to find what a Python String Contains. In this lesson, we will focus on these functions and find/replace what a. Beside this lesson, if you would like to elarn how to find Python String Length you can visit related lesson.  

 

So what are the Python String Methods that are used for these purposes? These are:

 

 

Now, let’s focus on each of these functions that will Show as what a Python String Contains.

 


 

find() Method

 

To find a string in a string the first method that we can use is python string find() method. With find() method, we can find any string in a given string. There are three parameters in find() method. These are:

 

  • Value
  • Start
  • End

 

The value is the mandatory part fort his method. It is the string that we are looking for. Beside this parameters, there is also a start and end parameter. These paramaters are the indexes that can be mentioned before this search. This means that look for between these indexes if Python String Contains it or not.

 

The output of this function is the first index value of the string. If it does not find the value, it returns with -1.

 

Let’s do an example about python string find () method.

 

msg = "Istanbul is in Turkey."
print(msg.find("Turkey"))

 

The output of this code is :

 

15

 

python-string-contains-find

 

 

Now, let’s see this example that find() method will not find the string.

 

msg = "Istanbul is in Turkey."
print(msg.find("France"))

 

The output of this python code will be:

 

-1

 

python-string-find-ipcisco-1

 

We can also define the other parameters of this fucntion. Here, we will define start and end index like below:

 

msg = "Istanbul is in Turkey."
print(msg.find("Turkey", 10, 21))

 

The output of this python string find code will be:

 

15

 

But what if we change these indexes? For example let’s change firstly the end index.

 

msg = "Istanbul is in Turkey."
print(msg.find("Turkey", 10, 17))

 

Because of the fact that the string that we are looking  for starts at index 15 and go through index 21, when we change this end index as 17, it will not find it . So the output will be:

 

-1

 

It will give the same output for the below code also. Because it is not containing 15-21 indexes.

 

msg = "Istanbul is in Turkey."
print(msg.find("Turkey", 4, 10))
-1

 

 


 

index() Method

 

index() method is another method to find if a Python String Contains any string. This function si similar to find() method. The only differrence between these two functions is, python find() method returns -1 if it do not find anything. But python index() method returns with an exception.

 

index() method has the same three parameters like find() method:

  • Value
  • Start
  • End

 

We can use the below example as the first find() method example.

 

msg = "Istanbul is in Turkey."
print(msg.index("Turkey"))

 

index() method finds this value and returns with the beginning index.

 

15

 

python-string-index-ipcisco-1

 

If the method do not find any value, then it returns with an exception.

 

msg = "Istanbul is in Turkey."
print(msg.index("France"))

 

Traceback (most recent call last):
  File "./prog.py", line 2, in <module>
ValueError: substring not found

 

count() Method

 

python count() method is another method used to find any string in a string. This time, python count() method finds the stings and returns with count of this string in the main string.

 

Like find() method and index() method, count() method also has three paremeters. They are dimilar to the parameters ued with these methods:

  • Value
  • Start
  • End

 

Let’s give an example and Show the output of this count() function.

 

msg = "I like dogs. Because dogs are very friendly"
print(msg.count("dogs"))

 

The output of this python code will be:

 

2

 

python-string-contains-count

 

If we use the other two parameters like below, it will find only one keyword between the mentioned indexes. So it will return as 1.

 

msg = "I like dogs. Because dogs are very friendly"
print(msg.count("dogs", 5,12))
1

 


replace() Method

 

replace() method is the last method that is used to find and replace a string in a string. We have talked about in another lesson detailly but here we will also explain this method again.

 

There are three paremeters of python replace() method. These are:

 

  • Old Value
  • New Value
  • Count

 

Old value is the string that will be changed. New value is the new string that will be replaced with old value. And the count is the number of this change. Here, the first two one is mandatory and the count parameter is optional parameter.

 

Let’s see the below example. In this example, we will use “dogs” as old parameter and “cats” as new parameter. We will not use the third count parameter here.

 

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

 

The output of this python code will be like below:

 

I like cats. Because cats are cute.

 

python-string-contains-replace

 

Now, we will use also the other parameters with another examples of python string replace method. Here, we will change two word with three but only for two times.

 

msg = "I have two birds, two dogs, two cats."
newmsg = msg.replace("two", "three", 2)
print(newmsg)

 

So, the output of this Python String Replace code will be like below:

 

I have three birds, three dogs, two cats.

 

As you can see here, only the first two words are changed as three. And the last one is remaining.

 

 

In this lesson, we have talked about if a Python String Contains any value and how can we find this. We have talked about four python methods used in python fort his purposes.

 

 

Back to: Python Programming Course > Python Strings

Leave a Reply

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

Python Programming Course

Collapse
Expand
Latest Blog Posts