3

I'd like to capitalize the first letter in a string. The string will be a hash (and therefore mostly numbers), so string.title() won't work, because a string like 85033ba6c would be changed to 85033Ba6C, not 85033Ba6c, because the number seperates words, confusing title(). I'd like to capitalize the first letter of a string, no matter how far into the string the letter is. Is there a function for this?

tkbx
  • 15,602
  • 32
  • 87
  • 122
  • What code did you try? – Aswin Murugesh Jun 14 '13 at 15:52
  • if you change a letter in a hash, it changes its meaning, rendering it virtually useless. – njzk2 Jun 14 '13 at 15:54
  • @njzk2 I'm not using it to check the validity of anything, just to create meaningless data out of meaningful data – tkbx Jun 14 '13 at 15:56
  • 4
    Did anyone read the post before marking as duplicate ? – Hunter McMillen Jun 14 '13 at 15:59
  • 1
    @njzk2 If you capitalize a letter in a hash encoded as hexadecimal digits, as is very, very, very, very, very common, the meaning doesn't change. – millimoose Jun 14 '13 at 16:06
  • @BlaXpirit -- That's true :) – mgilson Jun 14 '13 at 16:07
  • 1
    Did anyone read the post before reopening? – Oleh Prypin Jun 14 '13 at 16:08
  • @BlaXpirit: the problem is, the answers there are not particularly helpful. – georg Jun 14 '13 at 16:10
  • 1
    @thg435 ...How so? The accepted one solved the problem. – millimoose Jun 14 '13 at 16:10
  • @millimoose: "solved the problem" is not enough for StackOverflow. It must be clean, smart, concise, robust and helpful for everyone else. Just like the Ashwini's one below. – georg Jun 14 '13 at 16:13
  • @thg435 Those are subjective qualities that you could split hairs over ad nauseam to the point of making the flagging meaningless. – millimoose Jun 14 '13 at 16:16
  • @millimoose: Let's not question the obvious: the answer here is far better than there. So it was a good thing that we reopened and answered the question. – georg Jun 14 '13 at 16:24
  • @thg435 _every_ answer here is better than there – tkbx Jun 14 '13 at 16:32
  • @thg435 Thing is, I don't see the "far better". It's more robust (it handles the one known edge case), at the cost of being less straightforward, also doesn't handle Unicode input (which is not relevant for the stated use case but still) and the whole thing is a wash and hopefully you see what I mean by hair splitting now. (And I also consider robustness a "nice to have", not a requirement for SO answers.) – millimoose Jun 14 '13 at 16:33
  • @tkbx: thanks ;) Although I'd rather delete mine as it's basically a dupe of Ashwini's. – georg Jun 14 '13 at 16:35

2 Answers2

10

Using re.sub with count:

>>> strs = '85033ba6c'
>>> re.sub(r'[A-Za-z]',lambda m:m.group(0).upper(),strs,1)
'85033Ba6c'
Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
6

It is assumed in this answer that there is at least one character in the string where isalpha will return True (otherwise, this raises StopIteration)

i,letter = next(x for x in enumerate(myhash) if x[1].isalpha())
new_string = ''.join((myhash[:i],letter.upper(),myhash[i+1:]))

Here, I pick out the character (and index) of the first alpha character in the string. I turn that character into an uppercase character and I join the rest of the string with it.

mgilson
  • 300,191
  • 65
  • 633
  • 696