# Check if a string exists in a PDF file in Python

Hello Everyone!
We are going to learn about how to check if a string exists in a PDF file in Python, in this tutorial. Strings play an important role in Python. It is used in Projects, Applications, Software, etc.

Most of the time, we need to check if a string exists in a PDF file or not. So, here we will discuss how to check.

## To check if a string exists in a PDF file in Python

Here we will discuss two ways to solve this problem.
First way: We can check directly from PDF if a string exists or not.
Second way: We can check line by line if a string exists in a PDF file or not.

Suppose the PDF file contains the text below :

```We are going
to check
if a string
is exists in this PDF
or not.```

The PDF file is saved as Code.pdf.

### To check directly in the PDF file

We can directly check from the PDF if a string exists or not.

```St = 'check'

f = open("Code.pdf", "r")

if St in a:
print('String \'', St, '\' Is Found In The PDF File')
else:

f.close()```

Output:

`String ' check ' Is Found In The PDF File`

First, we need to open the file and store it in the ‘f’ variable. Then read the file and store it in the ‘a’ variable. Thereafter, it will print the output if the string is found or not. At last, it will close the file.

### To check line by line in the PDF

We can check line by line if a string exists in a PDF file or not.

```St = 'check'

f = open("Code.pdf", "r")

c = 0
line = 0

for a in f:
line = line + 1

if St in a:
c = 1
break

if c == 0:
else:
print('String \'', St, '\' Is Found In Line', line)

f.close()
```

Output:

`String ' check ' Is Found In Line 2`

First, We open a file and store in ‘f’ variable. Set zero to counter and line variable. Then assign a for loop to check it line by line. Display the output if the string is present or not. At last, we will close the file.

There are many ways to solve this problem. These are the approaches to check if a string exists in a PDF file.

Thank you.