Python String Casefold() Method

Python Casefold() method returns a lowercase copy of the string. It is more simillar to lowercase method except it revomes all case distinctions present in the string.

For example in German, ‘β’ is equivelent to “ss”. Since it is already in lowercase, lowercase do nothing and prints ‘β’ whereas casefold converts it to “ss”.

Python String casefold() Method Syntax

It has the following syntax:

casefold()  

    Parameters

    No parameter is required.

    Return Type

    It returns lowercase string.

    Python Version

    This function was introduced in Python 3.3.

    Different Examples for Python Sring casefold() Function

    Here, we are going to take some examples to demonstrate the working of string casefold() function in Python.

    Python String Casefold() Method Example 1

    Let us take an example to illustrate the string casefold() function in Python.

    # Python casefold() function example  
    
    # Variable declaration  
    
    str = "PYTHONAPP"  
    
    # Calling function  
    
    str2 = str.casefold()  
    
    # Displaying result  
    
    print("Old value:", str)  
    
    print("New value:", str2)

    Output:

    Old value: PYTHONAPP
    New value: pythonapp
    

    Python String Casefold() Method Example 2

    The strength of casefold, it not only converts into lowercase but also converts strictly. See an example below ‘β’ is converted into “ss”.

    # Python casefold() function example  
    
    # Variable declaration  
    
    str = "PYTHONAPP - β"  
    
    # Calling function  
    
    str2 = str.casefold()  
    
    # Displaying result  
    
    print("Old value:", str)  
    
    print("New value:", str2)

    Output:

    Old value: PYTHONAPP - β
    New value: pythonapp ? ss
    

    Python String Casefold() Method Example 3

    If string is in camelcase, it still converts whole string into lowercase.

    # Python casefold() function example  
    
    # Variable declaration  
    
    str = "PyThOnApP"  
    
    # Calling function  
    
    str2 = str.casefold()  
    
    # Displaying result  
    
    print("Old value:", str)  
    
    print("New value:", str2)

    Output:

    Old value: PyThOnApP
    New value: pythonapp

    Comments

    Leave a Reply

    Your email address will not be published. Required fields are marked *