Python if statement string compare | Example code

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.1.3 (Community Edition)

Windows 10

Python 3.7

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


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.