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?
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
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