Min menu

Pages

Search functions in the str class for text handling in Python

function python  ()count 

Defining it returns a number representing the number of times the passed string found the place of the parameter subin the invoked script.



Function structure count .()

                  count(sub[, start[, end]])
	  


Python function parameters count .() 

  • subIt is a plain text of its type str, and it is the text we want to search for.

  • startAn optional parameter, you can pass in its place an integer specifying the number of the cell from which you want to start the search.

  • endAn optional parameter, you can pass in its place an integer specifying the number of the cell at which you want the search to stop.



return value of the function count() 

Returns a number representing the number of times the passed text found the place of the parameter subin the invoked text.


Example

Test.py
                    # s Here we have defined a text variable named
		  s = "Hello, my name is Mahamad. I'm a full stack developer."
	  
		  # s in the text in the variable 'a' here we have printed how many times the text is
		  print(s.count('a'))
	

We will get the following result when running.

5

Python   function find ()

Defining the find() function in Python

It searches in the text that called it for the number of the first digit, then finds the same text or letter that we pass to it in place of the parameter suband returns it.
If the text to be searched is not found, it will be returned1- .



Build the find() function in Python

                  find(sub[, start[, end]])
	  


Parameters of the find() function in Python

  • subIt is a plain text of its type str, and it is the text we want to search for.

  • startAn optional parameter, you can pass in its place an integer specifying the number of the cell from which you want to start the search.

  • endAn optional parameter, you can pass in its place an integer specifying the number of the cell before which you want to stop the search.



Return value of the find() function in Python

Returns the number of the first cell at which the same text or character that we pass to it was found in place of the parameter sub.
Returns 1- if the text to be searched is not found.


Example

Test.py
                    # s Here we have defined a text variable named
		  s = 'Python is a programming language. Python is easy to learn.'
	  
		  # So it will print 0 'P' then there is the text s Here we have printed the number of the first digits of the variable
		  print(s.find('P'))
	  
		  # So it will print 0 'Python' then there is the text s Here we have printed the number of the first digit in the variable
		  print(s.find('Python'))
	  
		  # Noting that we started the search process from field number 10. Thus, 34 'Python' will be printed, then the text s is here. We have printed the number of the first cell in the variable
		  print(s.find('Python', 10))
	  
		  # -1 Since there is no such word, 'java' will be printed. Then there is the text s. Here we have printed the number of the first digits of the variable.
		  print(s.find('Java'))
	

We will get the following result when running.

0
0
34
-1

Python  functionrfind()

Defining the rfind() function in Python

You start searching from the end of the text that first called it for the number of the first digit, then the same text or letter that we pass to it is found in place of the parameter suband returns it.
If the text to be searched is not found, it will be returned1- .
The difference between it and the function find()is that it starts the search process from the last field in the text that called it to the first field in it.



Build the rfind() function in Python

                  rfind(sub[, start[, end]])
	  


Parameters of the rfind() function in Python

  • subIt is a plain text of its type str, and it is the text we want to search for.

  • startAn optional parameter, you can pass in its place an integer specifying the number of the cell at which you want to stop the search.

  • endAn optional parameter, you can pass in its place an integer specifying the number of the cell before which you want to start the search.



Return value of the rfind() function in Python

Returns the number of the first cell at which the same text or character that we pass to it was found in place of the parameter sub.
Returns 1- if the text to be searched is not found.


Example

Test.py
                    # s Here we have defined a text variable named
		  s = 'Python is a programming language. Python is easy to learn.'
	  
		  # So it will print 0 'P' then there is the text s Here we have printed the number of the first digits of the variable
		  print(s.rfind('P'))
	  
		  # So it will print 0 'Python' then there is the text s Here we have printed the number of the first digit in the variable
		  print(s.rfind('Python'))
	  
		  # Noting that we started the search process from the last field to the number 10. Thus, 34 'Python' will be printed, then the text s is here. We have printed the number of the first cell in the variable
		  print(s.rfind('Python', 10))
	  
		  # -1 Since there is no such word, 'java' will be printed. Then there is the text s. Here we have printed the number of the first digits of the variable.
		  print(s.rfind('Java'))
	

We will get the following result when running.

34
34
34
-1

Python Strings  index() function

Defining the index() function in Python

It searches in the text that called it for the number of the first digit, then finds the same text or letter that we pass to it in place of the parameter suband returns it.
If the text to search is not found, an exception is thrownValueError .



Build the index() function in Python

                  index(sub[, start[, end]])
	  


Parameters of the index() function in Python

  • subIt is a plain text of its type str, and it is the text we want to search for.

  • startAn optional parameter, you can pass in its place an integer specifying the number of the cell from which you want to start the search.

  • endAn optional parameter, you can pass in its place an integer specifying the number of the cell before which you want to stop the search.



Return value of the index() function in Python

Returns the number of the first cell at which the same text or character that we pass to it was found in place of the parameter sub.



Possible errors

Throws a ValueError if the text to search is not found.



Example

Test.py
                    # s Here we have defined a text variable named
		  s = 'Python is a programming language. Python is easy to learn.'
	  
		  # So it will print 0 'P' then there is the text s Here we have printed the number of the first digits of the variable
		  print(s.index('P'))
	  
		  # So it will print 0 'Python' then there is the text s Here we have printed the number of the first digit in the variable
		  print(s.index('Python'))
	  
		  # Noting that we started the search process from field number 10. Thus, 34 'Python' will be printed, then the text s is here. We have printed the number of the first cell in the variable
		  print(s.index('Python', 10))
	  
		  # ValueError Since there is no such word, an error will be shown with the title 'java'. The text s is here. We have printed the number of the first digits of the variable.
		  print(s.index('Java'))
	

We will get the following result when running.

0
0
34
  File "C:/Users/Mhamad/PycharmProjects/myapp/Test.py", line 14, in <module>
    print(s.index('Python'))
ValueError: substring not found

Python  functionrindex()

Defining the rindex() function in Python

You start searching from the end of the text that first called it for the number of the first digit, then the same text or letter that we pass to it is found in place of the parameter suband returns it.
If the text to search is not found, an exception is thrownValueError .
The difference between it and the function index()is that it starts the search process from the last field in the text that called it to the first field in it.



Build the rindex() function in Python

                  rindex(sub[, start[, end]])
	  


Parameters of the rindex() function in Python

  • subIt is a plain text of its type str, and it is the text we want to search for.

  • startAn optional parameter, you can pass in its place an integer specifying the number of the cell at which you want to stop the search.

  • endAn optional parameter, you can pass in its place an integer specifying the number of the cell before which you want to start the search.



Return value of the rindex() function in Python

Returns the number of the first cell at which the same text or character that we pass to it was found in place of the parameter sub.


Example

Test.py
                    # s Here we have defined a text variable named
		  s = 'Python is a programming language. Python is easy to learn.'
	  
		  # So it will print 0 'P' then there is the text s Here we have printed the number of the first digits of the variable
		  print(s.rindex('P'))
	  
		  # So it will print 0 'Python' then there is the text s Here we have printed the number of the first digit in the variable
		  print(s.rindex('Python'))
	  
		  # Noting that we started the search process from the last field to the number 10. Thus, 34 'Python' will be printed, then the text s is here. We have printed the number of the first cell in the variable
		  print(s.rindex('Python', 10))
	  
		  # -1 Since there is no such word, 'java' will be printed. Then there is the text s. Here we have printed the number of the first digits of the variable.
		  print(s.rindex('Java'))
	

We will get the following result when running.

34
34
34
-1