Skip to content

Python if statement string compare | Example code

  • by

Strings in Python are compared with == and != operators in if statement .If two Python strings are equivalent return True or not matched then False.

Example code if a statement with the string Python

Simple python example code. Use if statement to compare string is equal or not equal?

x = 'A'
if x == 'c':
    print("X == C")
elif x == 'A':
    print("X == A")
else:
    print("Not Matched")

Output:

More Conditional Expressions

MeaningMath SymbolPython Symbols
Less than<<
Greater than>>
Less than or equal<=
Greater than or equal>=
Equals===
Not equal!=

Do comment if you have any doubts and suggestions on this Python condition statement topic.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Your email address will not be published. Required fields are marked *