Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I case fold a string in Python 2?

Python 3.3 adds the casefold method to the str type, but in 2.x I don't have anything. What's the best way to work around this?

like image 410
Devin Jeanpierre Avatar asked Aug 16 '13 10:08

Devin Jeanpierre


People also ask

How do you make a string case sensitive in python?

Approach No 1: Python String lower() Method The lower() method converts all the characters in a string to the lowercase, making it easier to compare two strings.

How do you avoid lowercase and uppercase in Python?

Class names and constants are written in uppercase. We can ignore case in Python using the . upper() and . lower() methods.


2 Answers

Check out py2casefold.

>>> from py2casefold import casefold
>>> print casefold(u"tschüß")
tschüss
>>> casefold(u"ΣίσυφοςfiÆ") == casefold(u"ΣΊΣΥΦΟσFIæ") == u"σίσυφοσfiæ"
True
like image 182
Russ Avatar answered Oct 05 '22 21:10

Russ


There is a thread here which covers some of the issues (but may not resolve all), you can judge whether it is suitable for what you need. If this is no good then there are some useful tips for implementing case folding on the W3C site here.

like image 38
ChrisProsser Avatar answered Oct 05 '22 22:10

ChrisProsser