Check if a string exists in a PDF file in Python Last Updated : 28 Apr, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report In this article, we'll learn how to use Python to determine whether a string is present in a PDF file. In Python, strings are essential for Projects, applications software, etc. Most of the time, we have to determine whether a string is present in a PDF file or not. Here, we'll discuss how to check f a string exists in a PDF file in Python. Here, we'll talk about two approaches to fixing this issue. In, the First method we can determine whether a string exists or not directly from the PDF. Whereas, In the second method we will determine whether a string is present in a PDF file line by line. Let's say the following text is present in the PDF file: Code.pdfCheck the string from the PDF document We can directly check from the PDF if a string exists or not. We must first open the file and save its contents to the variable "f." After that, read the file and put the data in the variable "a." If the string is found, it will then print the outcome. Finally, the file will be closed. Python3 St = 'suraj' f = open("file1.pdf", "r") a = f.read() if St in a: print('String \'', St, '\' Is Found In The PDF File') else: print('String \'', St, '\' Not Found') f.close() Output: String ' geeksforgeeks ' Is Found In The PDF File Check the string from the PDF with its line number We can check line by line if a string exists in a PDF file or not. We first open a file and store the contents in the "f" variable. Set the line variable and counter both to zero. A for loop is then assigned to check it line by line. If the string is found, it will then print the outcome. Finally, the file will be closed. Python3 St = 'suraj' f = open("file1.pdf", "r") c = 0 line = 0 for a in f: line = line + 1 if St in a: c = 1 break if c == 0: print('String \'', St, '\' Not Found') else: print('String \'', St, '\' Is Found In Line', line) f.close() Output: String ' geeksforgeeks ' Is Found In Line 1 Comment More infoAdvertise with us Next Article How to check if a file already exists in R ? M mailsaihjar Follow Improve Article Tags : Python Practice Tags : python Similar Reads How to check if a file already exists in R ? In this article, we will discuss how to check if a file already exists or not using R Programming Language. Directory in use: Check the names of every file in the working directoryFirst, we will check how many files are available in our directory so for that, we will use list.files() function so it 2 min read How to check if a file exists in Ruby? This article will discuss how to check if a file exists in Ruby. Checking if a file exists is important whether you are managing file operations, creating applications or handling data. The intention of this guide however is to offer a detailed account of methods that can be used to effectively achi 2 min read Check if a given string is binary string or not - Python The task of checking whether a given string is a binary string in Python involves verifying that the string contains only the characters '0' and '1'. A binary string is one that is composed solely of these two digits and no other characters are allowed. For example, the string "101010" is a valid bi 3 min read Python - How to Check if a file or directory exists Sometimes it's necessary to verify whether a dictionary or file exists. This is because you might want to make sure the file is available before loading it, or you might want to prevent overwriting an already-existing file. In this tutorial, we will cover an important concept of file handling in Pyt 5 min read How to check if a Python variable exists? Checking if a Python variable exists means determining whether a variable has been defined or is available in the current scope. For example, if you try to access a variable that hasn't been assigned a value, Python will raise a NameError. Letâs explore different methods to efficiently check if a va 3 min read Check if element exists in list in Python In this article, we will explore various methods to check if element exists in list in Python. The simplest way to check for the presence of an element in a list is using the in Keyword. Example:Pythona = [10, 20, 30, 40, 50] # Check if 30 exists in the list if 30 in a: print("Element exists in the 3 min read Like