String manipulation: partially convert to lowercase

As the title says: What is the best way to convert a string from any case to lowercase, keeping the part intact? for example, a line like: FormatDate(%M)==2or stArTDate(%Y/%m)==11/3, and I want to convert it to FormatDate(%M)==2or stArTDate(%Y/%m)==11/3, that is, change it to lowercase, except for the part between the curly braces (). For the first example, I approached something like this:

>>> import re
>>> fdt = re.compile('(F|f)(O|o)(R|r)(M|m)(A|a)(T|t)(D|d)(A|a)(T|t)(E|e)\(')
>>> ss = "forMatDate(%M)==2"
>>> if fdt.match(ss):
...   SS = ss.split('(')
...   SS[0] = SS[0].lower()
...   ss = "(".join(SS)
... 
>>> print ss
formatdate(%M)==2

While it works perfectly, I didn’t really like to do this. A regular expression is ugly, and it practically limits a particular line. Is there a better (hence dynamic) way to do this? Thanks in advance. Hooray!!


Update:

, , : formatdate(), startdate() enddate() UserName==JohnDee ... ( ), , - . , Krumelur's script.

>>> fdt = re.compile('\(%[dmwyMW].*\)')
>>> ss = "formatDate(%M)==4"
>>> st = "UserName==JohnDee"
>>>
>>> def dt_lower(sX):
...   if fdt.search(sX):
...     p1,p2 = sX.split('(',1)
...     sX = "%s(%s" % (p1.lower(), p2)
...   else: sX = sX.lower()
...   return sX
... 
>>> print dt_lower(ss)
formatdate(%M)==4
>>>
>>> print dt_lower(st)
username==johndee

, . . !!

+3
5

? , , :

p1,p2 = instr.split('(',1)
lc = '%s(%s' % (p1.lower(), p2)
+5

, "Pythonic", :

Python 2.7.1 (r271:86832, May 27 2011, 21:41:45) 
[GCC 4.2.1 (Apple Inc. build 5664)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> ss = "forMatDate(%M)==2"
>>> if ss.lower().startswith('formatdate'):
...     i,j = ss.split('(', 1)
...     ss = '('.join((i.lower(), j))
... 
>>> ss
'formatdate(%M)==2'

, built-in string methods.

Python 3.2.

+3

if ss.lower().startswith('formatdate'):
+2

, ? :

x, y = ss.split('(')
'('.join((x.lower(), y))
+2

Krumerlur ,

p1,p2 = inst.split( '(' , 1 )

+2

All Articles