![]() Since Python uses Unicode to represent strings, all rules defined in the Unicode Standard apply to the Python as well. That principle works fine in almost all use-cases, but there are some instances where you should consider using the str.casefold() method instead.įor example, when implementing caseless matching of two strings, the str.casefold() is the way to go. Starting with Python 3.0, strings are represented as arrays of Unicode characters which makes it easy for str.lower() to replace every code of a capital letter with the code of the corresponding lowercase letter. It does what it is supposed to do by following a few simple rules. The previous method is suitable for most use cases. Why Use str.casefold() instead of str.lower() Also, you can check if a string is all-lowercase or all-uppercase by using the str.islower() or str.isupper(). It is used in the same fashion as the str.lower(). Note: The opposite method to the str.lower() method is the str.upper(). This piece of code will output: "Lowercase string EQUAL to expected string!" Print( "Lowercase string NOT EQUAL to expected string!") Print( "Lowercase string EQUAL to expected string!") Just to make sure that the str.lower() method produces the correct output, let's compare the lowercaseString to the expected lowercase version of the exampleString: if(lowercaseString = " $"): Only the letters can be converted into lowercase, so you would expect the lowercase version of this string to be " $": lowercaseString = exampleString.lower()Īfter calling the str.lower() method on the exampleString, its lowercase copy is stored as a new object, referenced by lowercaseString. Let's assume that you have some string that you want to convert: exampleString = " $"Īs you can see, this string has both lowercase and uppercase letters, as well as some special characters. Now, let's see how to convert a string to lowercase in Python. That can be useful when you want to save the original string for later use. The str.lower() method returns a lowercase copy of the string on which it is called. ![]() ![]() It is defined as a method of the String type which is built into the Python namespace. str.lower() is the built-in method made specifically for that purpose. How to Convert String to Lowercase in PythonĬonverting strings to lowercase is pretty straightforward to perform in Python. After reading the article, you'll be able to convert any string to lowercase, know when to simply use the str.lower() method, and when to choose the alternative approach instead. We'll discuss one alternative approach for converting strings to lowercase, so that you have a comprehensive overview of the subject. However, and especially if you're new to Python - read on. In this article we'll show you how to convert text to lowercase using one of the Pythons' built-in methods used to manipulate strings - str.lower().įrom a top-level view, the process is achieved through: exampleString = " $" Converting text into a uniform case is a good starting point of any type for text processing. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |