If char not in alphabet or alphabet.index(char) is the new variable name that stores all the characters from the variable.ĬASE I: strings containing uppercase letters only.īefore converting the uppercase letters to lowercase, we need to check if each character from the word is in uppercase.Īccording to the Unicode chart, the capital letter A has the number “65”, and the capital letter Z has the number “90”. These characters are added to the variable name that stores an empty string. To check if a character is lowercase, we find the index and compare it to the last count of the lowercase letters in the list.Īgain, lowercase letters have indexes from 0-25, and the index of the last lowercase letter is 25. To check if a character is not a letter, we use the “not in” keyword. If some letters in the word are lowercase, they will be left unchanged.Īfter these checks, it assumes the remaining characters are uppercase letters. has both uppercase and lowercase letters in the word.The conditions will check if each character from the word: This is what the code looks like when we bring it all together: def change_to_lowercase(word):ĬASE II: strings with special symbols, numerals, lowercase letters alongside uppercase letters.īefore converting the uppercase letters to lowercase, there are some conditions we need to check. We return the variable by printing it outside the loop. The lowercase letters are now added to the variable name that stores an empty string. When we get the indexes of the lowercase numbers, we use indexing (variable_name) to find the corresponding letters. To get the corresponding lowercase letters, we substract 26 from each uppercase index. When setting the condition ("if" statement) we start checking if the index of the letter is greater than '25' because the first uppercase index starts from '26'. In the list, the lowercase letters have indexes from 0-25 and uppercase letters have indexes from 26 - 51. In the above code, the indexes of the letters in the "GIRL" are printed. To find an index we use the ".index()" method: alphabet = To convert the uppercase letters to lowercase, we have to find the index of each letter stored by the variable from the list. The first case is the strings with only uppercase letters and the second has strings with special symbols, numerals, some lowercase, and some uppercase letters. There are two cases of strings we are going to convert. is the new variable name that stores all the characters from the variable. Next, we loop through each character in the string. The indexes of the lowercase letters range from 0 - 25, while the indexes of the upper case letters ranges from 26 - 51. We can also see that the lowercase letters are written first (left side), and the uppercase letters are written second (right side). There are 26 letters in the English alphabet, but the index in a list starts from 0, so the count of the alphabet is 51 (for both upper and lowercase letters). In the list above, we see that it has lowercase letters and uppercase letters. word = str(input("Enter a word: ” ))Īlphabet = Last, create the final variable that stores an empty string, which is where the lowercase letters will be stored. Then, create another variable that stores a list of uppercase letters and lowercase letters. The idea is to loop through a list of letters and replace the uppercase letters in a string with lowercase letters.įirst, create a variable that stores an input that accepts a string with uppercase letters. In this article, we will look at two different ways. Other Ways to Convert a Python String to LowercaseĪpart from the inbuilt method “.lower()”, there are different ways of converting uppercase letters to lowercase letters in Python. lower() method, it converts those letters to lowercase. We can see in the above code block that the variables that store each string have uppercase letters. The “.lower() “ method changes the strings to lowercase. It also applies to strings that have letters both in uppercase and lowercase. In Python, there is a built-in method that can change a string that is in uppercase to lowercase. When changing a string to lowercase, it only applies to the letters. You can write the English alphabet as uppercase or lowercase letters. Strings can consist of different characters – one of those characters being letters of the alphabet. How to Convert a String to Lowercase using. In this article, we will learn how to convert uppercase letters to lowercase letters without using the built-in method. title() method to capitalize the first letter. These methods are built-in functions that change the results of the string.įor instance, if I want to print out my name with its first letter capitalized, I use the. In Python, there are different ways of working with strings. These characters can be letters, symbols, or numbers. A string is a datatype that consists of characters wrapped in quotation marks.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |