-3

I have been using the following code to capitalize words:

with open("capitalize.txt") as f:
for line in f:
    print line.title(),

It works fine but I want to be able to capitalize letters in the middle of the string e.g change javascript to JavaScript, how can I do this using python?

Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321
  • 6
    Do you have a rule you're following to decide which letter to capitalize? – colcarroll Feb 20 '14 at 19:33
  • 5
    I don't think you can do this without ambiguity. For instance, what is the appropriate capitalization for "expertsexchange"? Is it "ExpertsExchange", or "ExpertSexChange"? – Kevin Feb 20 '14 at 19:34
  • What version of Python are you using and is it 32 or 64 bit? – wnnmaw Feb 20 '14 at 19:35
  • Actually, this could get you on the right track: http://stackoverflow.com/questions/9453820/alternative-to-python-string-item-assignment – colcarroll Feb 20 '14 at 19:37
  • This should also be helpful: http://stackoverflow.com/questions/1291734/finding-dictionary-words – wnnmaw Feb 20 '14 at 19:41
  • @wnnmaw 2.7.3 and ubuntu 64bit – Padraic Cunningham Feb 20 '14 at 19:44
  • @JLLagrange, as stated below, it is part of a codeeval problem where the outline states javascript should become JavaScript – Padraic Cunningham Feb 20 '14 at 19:46
  • @PadraicCunningham, I'm afraid I know nothing about Python on Ubuntu. I was going to suggest making a compound word splitter using a spell check package. This would give you a semi-intelligent way to split "javascript" into "java" and "script" though you would never be free of ambiguity. Once you split them it would be as simple as ```camelCase = "".join(map(lambda x: x.title(), wordList))``` – wnnmaw Feb 20 '14 at 19:50
  • @wnnmaw, I have actually just found the problem, I am changing words that already have capitalization mid-word by using line.title() so the problem lies in how I am capitalizing the strings. – Padraic Cunningham Feb 20 '14 at 19:55
  • @PadraicCunningham So do you need help figuring out how to capitalize the first letter while leaving the rest untouched? – wnnmaw Feb 20 '14 at 20:40
  • @wnnmaw, I figured that bit out , thanks for the help. – Padraic Cunningham Feb 20 '14 at 21:34

1 Answers1

4

It seems that you're not describing an algorithmic transformation (eg first letter, last letter, word boundaries, etc) but rather an arbitrary capitalization scheme in the context of known words.

As such, you'll probably want a permutation of the following using replace:

with open("capitalize.txt") as f:
    for line in f:
        print line.replace("javascript", "JavaScript")

If you've got a known set of words, then you can make it fancier, such as creating a dict {'javascript': 'JavaScript'} and then looping through the keys replacing each key with its value, but the basic approach will be more manual than you're envisioning.

DreadPirateShawn
  • 8,164
  • 4
  • 49
  • 71
  • that is what I feared! It is for a codeeval problem and took me ages to realize I was printing Javascript instead of JavaScript described in the problem outline. I thought it was strange to have to do it manually as the input file is random and not displayed. – Padraic Cunningham Feb 20 '14 at 19:42