Python make all lowercase

Python Convert String to Lowercase

Sometimes there could be scenarios where we may need to convert the given string to lowercase as a step in pre-processing.

To convert String to lowercase, you can use lower() method on the String. In this tutorial, we will learn how to transform a string to lowercase.

Syntax

The syntax to use lower() method is:

String.lower() function returns a string with all the characters of this string converted to lower case.

Examples

1. Convert given string (with some uppercase characters) to lowercase

Following is an example python program to convert a string to lower case.

Python Program

mystring = 'Python Examples' print('Original String :',mystring) lowerstring = mystring.lower() print('Lowercase String :',lowerstring)
Original String : Python Examples Lowercase String : python examples

2. Convert given string (with all uppercase characters) to lowercase

In this example, we take a string with all uppercase alphabets. And convert the string to lowercase.

Python Program

mystring = 'HTTPS://PYTHONEXAMPLES.ORG' print('Original String :',mystring) lowerstring = mystring.lower() print('Lowercase String :',lowerstring)
Original String : HTTPS://PYTHONEXAMPLES.ORG Lowercase String : https://pythonexamples.org

Summary

In this tutorial of Python Examples, we learned to transform a given string to lowercase using string.lower() method, with the help of well detailed examples.

Источник

Convert a List to Lowercase in Python

Convert a List to Lowercase in Python

  1. Use the str.lower() Function and a for Loop to Convert a List of Strings to Lowercase in Python
  2. Use the map() Function to Convert a List of Strings to Lowercase in Python
  3. Use the Method of List Comprehension to Convert a List of Strings to Lowercase in Python

Lists can be utilized to stock multiple items in a single variable. In Python, we can create a list of strings in which the different elements contained in the list are enclosed within single or double quotes.

This tutorial demonstrates how to convert a list of strings to lowercase in Python.

Use the str.lower() Function and a for Loop to Convert a List of Strings to Lowercase in Python

The str.lower() method is utilized to simply convert all uppercase characters in a given string into lowercase characters and provide the result. Similarly, the str.upper() method is used to reverse this process.

Along with the str.lower() function, the for loop is also used to iterate all the elements in the given list of strings.

The following code uses the str.lower() function and the for loop to convert a list of strings to lowercase.

s = ["hEllO","iNteRneT","pEopLe"] for i in range(len(s)):  s[i] = s[i].lower() print(s) 

Use the map() Function to Convert a List of Strings to Lowercase in Python

Python provides a map() function, which can be utilized to apply a particular process among the given elements in any specified iterable; this function returns an iterator itself as the output.

Читайте также:  Javascript новое диалоговое окно

A lambda function can be defined as a compact anonymous function that takes any amount of arguments and consists of only one expression. The lambda function will also be used along with the map function in this method.

The following code uses the map() function and the lambda function to convert a list of strings to lowercase in Python.

s = ["hEllO","iNteRneT","pEopLe"] a = (map(lambda x: x.lower(), s)) b = list(a) print(b) 

Use the Method of List Comprehension to Convert a List of Strings to Lowercase in Python

List comprehension is a much shorter way to create lists to be formed based on the given values of an already existing list. This method basically creates a new list in which all the items are lowercased.

The following code uses list comprehension to convert a list of strings to lowercase.

s = ["hEllO","iNteRneT","pEopLe"] a = [x.lower() for x in s] print(a) 

Vaibhhav is an IT professional who has a strong-hold in Python programming and various projects under his belt. He has an eagerness to discover new things and is a quick learner.

Related Article — Python List

Источник

Python Lowercase String with .lower(), .casefold(), and .islower()

Python Lowercase a String Cover Image

Learn how to use Python to lowercase text using both the str.lower() and str.casefolds() methods.

By the end of this tutorial, you’ll learn when to use both methods, including forcing special characters such as ß to their lower case alternatives. You’ll also learn how to check if a string is already lowercase by using the str.islower() method and how to convert lists of strings to their lower case strings. You’ll also learn how to convert a Pandas dataframe column to lowercase.

Being able to work with strings is an incredibly useful skill to learn for any budding Pythonista or data scientist. Data science is rapidly moving more and more into text analysis, which requires text to be pre-processed. One of the many transformations you’ll often undertake is to convert text to its lowercase equivalent.

Knowing how to do this and how to check if strings are already lower case is an important skill to learn. Let’s get started!

The Quick Answer: Use .lower() and .casefold()

Quick Answer - Python Lowercase String

Python Lowercase String with lower

Python strings have a number of unique methods that can be applied to them. One of them, str.lower() , can take a Python string and return its lowercase version. The method will convert all uppercase characters to lowercase, not affecting special characters or numbers.

Python strings are immutable, meaning that the can’t be changed. In order to change a string to lowercase in Python, you need to either re-assign it to itself or to a new variable.

Let’s take a look at an example of how we can use the Python str.lower() method to change a string to lowercase:

# Convert a String to Lowercase in Python with str.lower() a_string = 'LeArN how to LOWER case with datagy' lower = a_string.lower() print(lower) # Returns: learn how to lower case with datagy

You could also re-assign the string to itself to prevent having to create a new variable. Let’s see how this would work:

# Convert a String to Lowercase in Python with str.lower() a_string = 'LeArN how to LOWER case with datagy' a_string = a_string.lower() print(a_string) # Returns: learn how to lower case with datagy

In the next section, you’ll learn about a similar string method, str.casefold() , which also helps convert special characters to lowercase in Python.

Читайте также:  Загрузка javascript до загрузки всей страницы

Python Lowercase String with casefold

Beginning in Python 3.3, Python introduced a new string method to lowercase text, str.casefold() . What makes str.casefold() different from str.lower() is that it uses aggressive case lowering.

Want to learn how to capitalize a string in Python in different ways? Check out my complete guide to capitalizing strings in Python.

While on the surface the two methods will yield nearly identical results, the str.casefold() method will also return “lowercase” versions of special characters. For example, the letter ß will remain the same when the str.lower() method is used. However, the str.casefold() method will return ss .

Let’s see how we can use str.casefold() method to lowercase a Python string:

# Convert a String to Lowercase in Python with str.casefold() a_string = 'LeArN how to LOWER case with datagy' a_string = a_string.casefold() print(a_string) # Returns: learn how to lower case with datagy

Finally, let’s compare the use of the lower() and casefold methods() with a practical example:

# Comparing Lowercasing Strings with .lower() and .casefold() a_string = 'LowerCase Strings WITH datagy and ßome special characters' lower = a_string.lower() casefold = a_string.casefold() print(f'') print(f'') # Returns: # lower='lowercase strings with datagy and ßome special characters' # casefold='lowercase strings with datagy and ssome special characters'

We can see here that the str.lower() method kept the ß character, while the str.casefold() method converted it to ss .

In the next section of this tutorial, you’ll learn how to check if a Python string is completely lowercase or not.

Check if a Python String is Lowercase with islower

Python makes it very easy to see if a string is already lowercase, using the str.islower() method. The method will return a boolean value:

  • True is the entire string is lowercase, and
  • False is the entire string isn’t a lowercase

If there is even a single element in the string that isn’t lower cased, the method will return False .

Let’s see how we can use Python to check if a string is lowercased:

# Checking if a string is lower case in Python a_string = 'LowerCase Strings WITH datagy' lowered_string = a_string.lower() # Check for lower case using .islower() print(a_string.islower()) print(lowered_string.islower()) # Returns: # False # True

In the next section, you’ll learn how to turn a list of strings to lowercase.

Convert a List of Strings to Lowercase

You may find yourself with a Python list of strings that you need to convert to lowercase.

We can do this with either the str.lower() or str.casefold() methods. We’ll loop over each item in the list and turn it to lower case.

Читайте также:  Php str to upper case

We’ll first go over how to accomplish this using a Python for loop:

# Convert a Python list of strings to lowercase with a for loop a_list = ['DaTAGY', 'iS', 'a', 'PLAce', 'to', 'LEArn', 'PythoN'] lowered = [] for item in a_list: lowered.append(item.lower()) print(lowered) # Returns: ['datagy', 'is', 'a', 'place', 'to', 'learn', 'python']

In order to make this happen, we first need to instantiate a new, empty list to hold our lowercased strings. We then loop over each string in the original list and append its lowered version to the new list.

Want to learn more about Python for-loops? Check out my in-depth tutorial that takes your from beginner to advanced for-loops user! Want to watch a video instead? Check out my YouTube tutorial here.

Now let’s take a look at how we can simplify this process by using a Python list comprehension:

# Convert a Python list of strings to lowercase with a list comprehension a_list = ['DaTAGY', 'iS', 'a', 'PLAce', 'to', 'LEArn', 'PythoN'] lowered = [item.lower() for item in a_list] print(lowered) # Returns: ['datagy', 'is', 'a', 'place', 'to', 'learn', 'python']

While this does essentially the same thing as the for loop, it does save us some lines of code and doesn’t require us to for initialize an empty list.

In the next section, you’ll learn how to use string methods to convert a Pandas column to lowercase.

Want to learn more about Python list comprehensions? Check out this in-depth tutorial that covers off everything you need to know, with hands-on examples. More of a visual learner, check out my YouTube tutorial here.

Convert a Pandas Dataframe Column to Lowercase

Being able to work with string data in Pandas is an important skill. There are many times when you’ll encounter string data in tabular forms and you’ll need to convert it all to lower case.

Let’s load a Pandas dataframe to start off:

# Loading a Sample Pandas Dataframe import pandas as pd df = pd.DataFrame.from_dict(< 'Numbers': [1,2,3,4,5,6,7], 'SomeStrings': ['DaTAGY', 'iS', 'a', 'PLAce', 'to', 'LEArn', 'PythoN'], >) print(df) # This returns: # SomeStrings Numbers # 0 DaTAGY 1 # 1 iS 2 # 2 a 3 # 3 PLAce 4 # 4 to 5 # 5 LEArn 6 # 6 PythoN 7

Now that we have a dataframe, let’s see how we can lowercase a column using Pandas string methods:

# Converting a Pandas column to Lower Case import pandas as pd df = pd.DataFrame.from_dict(< 'Numbers': [1,2,3,4,5,6,7], 'SomeStrings': ['DaTAGY', 'iS', 'a', 'PLAce', 'to', 'LEArn', 'PythoN'], >) df['SomeStrings'] = df['SomeStrings'].str.lower() print(df) # This returns: # Numbers SomeStrings # 0 1 datagy # 1 2 is # 2 3 a # 3 4 place # 4 5 to # 5 6 learn # 6 7 python

You can see here that the entire column’s text has been converted to lowercase. We accomplished this by using the str.lower() method to it, which helps access the string portion of the column.

Conclusion

In this tutorial, you learned how to use Python to convert strings to lowercase, using the str.lower() and the str.casefold() string methods. You also learned how to check if a string is already lowercase by using the str.islower() method. Finally, you learned how to convert a list of strings as well as a Pandas dataframe column to lowercase.

To learn more about the str.lower() method, check out the official documentation here.

To learn more about the str.casefold() method, check out the official documentation here.

Источник

Оцените статью