Building Cloud Expertise with centron - Our Tutorials

Whether you are a beginner or an experienced professional, our practical tutorials provide you with the knowledge you need to make the most of our cloud services.

Check if a String Contains Another String: Python Tips

String manipulation is a common task in any programming language. Python provides two common ways to check if a string contains another string.

Python Check if String Contains Another String

Python string supports the in operator. So we can use it to check if a string is part of another string or not. The in operator syntax is:

It returns True if “sub” string is part of “str”, otherwise it returns False. Let’s look at some examples of using the in operator in Python.

str1 = 'I love Python Programming'

str2 = 'Python'

str3 = 'Java'

print(f'"{str1}" contains "{str2}" = {str2 in str1}')
print(f'"{str1}" contains "{str2.lower()}" = {str2.lower() in str1}')
print(f'"{str1}" contains "{str3}" = {str3 in str1}')

if str2 in str1:
    print(f'"{str1}" contains "{str2}"')
else:
    print(f'"{str1}" does not contain "{str2}"')

Output:

"I love Python Programming" contains "Python" = True
"I love Python Programming" contains "python" = False
"I love Python Programming" contains "Java" = False
"I love Python Programming" contains "Python"

If you are not familiar with f-prefixed strings in Python, it’s a new way for string formatting introduced in Python 3.6. You can read more about it at f-strings in Python.

When we use the in operator, internally it calls __contains__() function. We can use this function directly too, however, it’s recommended to use the in operator for readability purposes.

s = 'abc'

print('s contains a =', s.__contains__('a'))
print('s contains A =', s.__contains__('A'))
print('s contains X =', s.__contains__('X'))

Output:

s contains a = True
s contains A = False
s contains X = False

Using find() to Check if a String Contains Another Substring

We can also use the string find() function to check if a string contains a substring or not. This function returns the first index position where the substring is found, else returns -1.

str1 = 'I love Python Programming'

str2 = 'Python'

str3 = 'Java'

index = str1.find(str2)
if index != -1:
    print(f'"{str1}" contains "{str2}"')
else:
    print(f'"{str1}" does not contain "{str2}"')

index = str1.find(str3)
if index != -1:
    print(f'"{str1}" contains "{str3}"')
else:
    print(f'"{str1}" does not contain "{str3}"')

Output:

"I love Python Programming" contains "Python"
"I love Python Programming" does not contain "Java"

Python breakpoint() function

Python breakpoint() is a new built-in function introduced in Python 3.7. Python code debugging has always been a painful process because of the tight coupling between the actual code and the debugging module code. For example, if you are using pdb debugger, then you will have to call pdb.set_trace() in your program code. If you want to use any other debugger, let’s say web-pdb then you will have to remove all the code related to PDB and add web_pdb.set_trace() method. This causes a huge overhead in using python debugger and makes the python code hard to debug and maintain. That’s why Python 3.7 has introduced breakpoint() method that allows us to write loosely coupled debugging code.

How Python breakpoint() Works

Python breakpoint() function calls sys.breakpointhook() function. By default, sys.breakpointhook() calls pdb.set_trace() function. So at the very least, using breakpoint() provide convenience in using a debugger because we don’t have to explicitly import pdb module. Let’s look at a simple example of breakpoint() function usage. We have a python script python_breakpoint_examples.py with the following code.

x = 10
y = 'Hi'
z = 'Hello'
print(y)

breakpoint()

print(z)

When we execute this script, the PDB debugger console opens up.

$python3.7 python_breakpoint_examples.py
Hi
> /Users/pankaj/Documents/PycharmProjects/BasicPython/basic_examples/python_breakpoint_examples.py(8)()
-> print(z)
(Pdb) c
Hello
$

Stopping Debugging with Python breakpoint()

Python sys.breakpointhook() function uses environment variable PYTHONBREAKPOINT to configure the debugger. If unset, the default PDB debugger is used. If it’s set to “0” then the function returns immediately and no code debugging is performed. It’s very helpful when we want to run our code without debugging.

$PYTHONBREAKPOINT=0 python3.7 python_breakpoint_examples.py
Hi
Hello
$

Changing Debugger Module with Python breakpoint()

We can use PYTHONBREAKPOINT environment variable to provide the debugger method to be called by breakpoint() function. This is very helpful because we can change the debugger module easily without making any code change. Let’s say we want to use web-pdb debugger. We can easily hook it into our program using PYTHONBREAKPOINT=web_pdb.set_trace. First of all, make sure that web-pdb is installed. You can install it using pip3.7 install web-pdb command.

$PYTHONBREAKPOINT=web_pdb.set_trace python3.7 python_breakpoint_examples.py
Hi
2018-08-10 12:49:54,339: root - web_console:110 - CRITICAL - Web-PDB: starting web-server on pankaj:5555...

Open the web-server URL provided in the console log and you will see the debugger window. We can issue PDB commands using this UI, you can send command “c” to continue and complete our program.

Summary

Python breakpoint() function is a very helpful addition to the python debugging feature. It’s recommended to use this for debugging so that you can easily hook other third-party debuggers on the fly. It also provides an easy option to disable the debugger and runs the program normally.

Start Your Cloud Journey Today with Our Free Trial!

Dive into the world of cloud computing with our exclusive free trial offer. Experience the power, flexibility, and scalability of our cloud solutions firsthand.

Try for free!