
Python String Position Find
In this lecture, we are going to learn βhow to Python String position Find in a Paragraph and check also which word exists or notβ In the last lecture, we learn about βwhat is a Find Operator in PythonβΒ. Before this lecture, we learn how to find a single character in a string. But, now we are going to learn how to find a word in a paragraph.
Let we take an example
How to Find Position of the Specific word in Python?
Example:1
name = "my name is ABDUL JABBAR and I am student of Computer Science."
position = (name.find("student"))#postion is variable that store position of "student"Β word in a paragraph.
print (position)
Output: 33
Note: Spaces between any word also count by Compiler. Output always show according to the first alphabet of the word which we want to print. Like βstudentβ word in the following program.
Β
Description: In the first line, we initialize a variable that is used to store string data type. In the second line, the position is a variable that holds the position of βstudentβΒ keyword from the paragraph. The third line is used to print the value.
How to check a word in the paragraph which exists or not?
We find a specific word whether it exists or not in the following paragraph. If the following word exists in the paragraph then the position (index of the word) will be shown. If a specific word does not exist in the paragraph then the output will be -1. In this program, we will use the Find Operator to check. Let we take an example
Example:2
name = "my name is ABDUL JABBAR and I am student of Computer Science."
position = (name.find("Doctor"))
print (position)
Output: -1
In the above program, we want to print βDoctorβ word that does not exist in the given paragraph. Thatβs why output will be -1.
I hope this article is very helpful. Share with your friend who wants to learn Python in an easy way. In the next lecture, we will learn about βuse number in Find operatorβΒ. You can also learn HTML5 and many other programming languages.Β If you have any question, please discuss in the comments section. Please also give your feedback. Thank You!.
You may also like