Last modified: Jan 10, 2023 By Alexander Williams

check if not startswith in python

In this tutorial, we're going to learn how to check if string not starts with with a spesific word.

So, let's get started.

Syntax


not my_str.startswith()

Method 1: Using 'if not'


#string
my_str = "Hello Python"

if not my_str.startswith('PHP'):
    print(True)
else:
    print(False)

In this example, we have the my_str string.

If my_str starts with PHP will return True if not, it will return False.

output:

True

Method 1: Using 'is False'

In the following example, we'll check if the string starts with "PHP" by using the 'is False' method.


#string
my_str = "Hello Python"

if my_str.startswith('PHP') is False:
    print(True)
else:
    print(False)

output:

True

Using inlne if statement


my_str = "Hello Python"

print(True if my_str.startswith('PHP') is False else False)

output:

True