pFad - Phone/Frame/Anonymizer/Declutterfier! Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

URL: https://www.askpython.com/python/string/python-string-casefold

et(e),r=t[e];if(!n||!r)continue;const c=decodeURIComponent(n),d="plaintext"===r.type&&i(c),s="hashed"===r.type&&c;if(d||s){a={value:c,config:r};break}}if(a){const{value:t,config:e}=a;window.adthrive=window.adthrive||{},window.adthrive.cmd=window.adthrive.cmd||[],window.adthrive.cmd.push(function(){window.adthrive.identityApi({source:e.source,[e.identityApiKey]:t},({success:i,data:n})=>{i?window.adthrive.log("info","Plugin","detectEmails",`Identity API called with ${e.type} email: ${t}`,n):window.adthrive.log("warning","Plugin","detectEmails",`Failed to call Identity API with ${e.type} email: ${t}`,n)})})}!function(t,e){const i=new URL(e);t.forEach(t=>i.searchParams.delete(t)),history.replaceState(null,"",i.toString())}(e,n)}()}(); Python String casefold() - AskPython

Python String casefold()

The Python String casefold() method returns a case folded string, and we use this to remove all case distinctions in a string.

Let’s look at what this means.


Using the Python String casefold() method

This belongs to the String class, and can be used only on String objects.

The Syntax for this method is:

string_obj.casefold()

This will return a new string, after applying all case folds.

For example, it will convert all uppercase letters to lowercase.

my_str = "Hello from AskPython"

casefolded_str = my_str.casefold()

print(casefolded_str)

Output

hello from askpython

This converts all uppercase letters to lowercase ones for the English alphabet.

But what if the string has characters from another language, and in another encoding? The Python string casefold() method solves this problem.

Take an example of the German lowercase letter ‘รŸ ‘. This corresponds to the English string “ss”, since there is an encoding for German alphabets, and we decode it to an English string.

If we were to use the lower() method on this alphabet, it would do nothing, since the letter is already in lowercase, so the output will remain ‘ รŸ ‘. Python string casefold() not only converts to lowercase, but also makes sure we get back our English string “ss”.

The below screenshot shows this difference.

German String Casefold
German String Casefold

Let’s take one more example, to show that the strings ‘รŸ‘ and “SS” will resolve to the same casefolded string “ss”.

s1 = 'รŸ'
s2 = 'ss'
s3 = 'SS'
if s1.casefold() == s2.casefold():
    print('Casefolded strings of s1 and s2 are equal')
else:
    print('Casefolded strings of s1 and s2 are not equal')

if s1.casefold() == s3.casefold():
    print('Casefolded strings of s1 and s3 are equal')

Output

Casefolded strings of s1 and s2 are equal
Casefolded strings of s1 and s3 are equal

Indeed, both these strings resolve to the same casefolded string!


Conclusion

In this article, we learned how we could use the Python string casefold() method in Python.


References


Vijaykrishna Ram
Vijaykrishna Ram
Articles: 99
pFad - Phonifier reborn

Pfad - The Proxy pFad © 2024 Your Company Name. All rights reserved.





Check this box to remove all script contents from the fetched content.



Check this box to remove all images from the fetched content.


Check this box to remove all CSS styles from the fetched content.


Check this box to keep images inefficiently compressed and original size.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy