TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to PYTHON

Python String Equals: casefold

Use the equals operator on strings. Specify casefold and lower in a string comparison.
Equals. Strings can be compared for exact equality with two equals signs. With this syntax, the characters (and their cases) are compared. If not equal, the test returns false.Strings
An example. Here we test strings for equality. We have several string literals in the program. We use two equals to check character data.

Casefold: Newer versions of Python support the casefold method. Similar to lower(), it handles Unicode characters better.

Lower: We can also use the lower method to standardize the casing of strings in our programs.

Python program that tests string equality value = "CAT" if value == "cat": print("A") # Not reached. if value == "CAT": print("B") if str.casefold(value) == "cat": print("C") if str.lower(value) == "cat": print("D") Output B C D
Equals, not equals. With the equals operator, the characters of two strings are compared. It does not matter if they are different object instances.
Python program that uses equals, not equals location1 = "forest" location2 = "FOREST".lower() # These two strings are equal. if location1 == location2: print("In forest") # These two strings are not equal. if location1 != "desert": print("Not in desert") Output In forest Not in desert
Some notes, performance. It is fastest to avoid creating temporary strings (as with the lower method) before testing equality. Usually, avoiding allocations helps performance.
A review. We can use the equals and not-equals operators to compare two strings. Often we do this in an if-statement, but the expression can be used in any part of a program.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf