. Advertisement .
. Advertisement .
Aren’t you familiar with the application of Python if statement with string? This post will explain how you can apply this statement in your coding process. Check it out now to gain more information about the statement.
The statement if is a conditional statement in programming that performs a task or shows data if it is validated as true. It can be utilized to execute a code block if a condition is satisfied. When used with the keyword else, which executes a code block in case the statement if’s condition isn’t met.
Application Of Python If Statement With String
Let’s discover the use of this statement with string in Python.
Compare A String To A String Set
A string in Python is a collection of characters that may each be accessed separately at a specific index. Utilizing the keyword in, you can compare a string to a group of strings. The string set may be represented in a list form, and if there is one match element, the block if will be executed.
Observe the following example.
a = 'y' if a in ['y', 'Y', 'yes', 'Yes', 'YES']: print("Match") else: print("No match")
Convert Inputs To One Single Form
As the Python programming language is case-sensitive, you must be certain of all potential matches. The other time-saving method is to convert the inputs to one single form, either in lowercase or uppercase, before running the statement if to check its condition. To convert a string to one case, utilize the
Below is a sample for your reference.
a = 'YES' if a.lower() in ['y','yes']: print("Match") else: print("No match")
The statement if allows you to compare strings. In this case, you may utilize relational operators with strings to run a basic comparison.
Check out the example below for a better understanding.
a = 'Hamed' b = 'Mark' if(a!=b): print("Not equal") else: print("Equal")
if(a<b): print("Two is greater") else: print("One is greater")
Not equal Two is greater
Notice that we carried out several procedures in the above sample.
Verity The Characters
By incorporating the
sorted() feature within the equality procedure, you may also determine if your characters are identical but not ordered. Strings may also be utilized with the operator is to check whether or not two objects are referring to the same item.
You can check the below sample code.
a = 'mnba' b = 'nbam' c = b if sorted(a) == sorted(b): print("Equal") else: print("Not equal")
if(c is b): print('True')
As you can see in the above sample, these two strings are equal once sorted correctly. Plus, the b and c strings are references to one string, which is the reason for the operator is to give back the True value.
The Bottom Line
This post has covered a lot of information about the Python if statement with string. We also explained the application of this programming statement. Hopefully, you find this guide useful and easy to understand.Suppose you want to learn more about the statement if in Python; check out this guide. It will show you how to exit this statement in case you do not need to use it anymore.
Leave a comment