简体   繁体   中英

(Python) Capitalize first letter of every word without using .title() or .capitalize()

I'm trying to find a way to capitalize the first letter of each word in a given phrase, excluding a few words (such as and, the, is) and it needs to be inputed by the user. Basically, I want to make a .title() program without using .title() or .capitalize(). Recreating .title() in a sense.

Example:

s = #split, join, upper, lower, etc. functions here 

s = input('Enter a sentence: ')

>>>Enter a sentence: the world and me
The World and Me

Edit: I have searched over multiple posts, none of which were able to answer the specific question I have.

The easiest way to split the string into words is words = s.split(' ') (although regular expressions is usually better).

Then you can iterate over the words and capitalize them if they're not whitelisted, and then rejoin them into a string:

words = s.split(' ')
whitelist = {'and', 'the', 'is'}
capitalized = ' '.join([word.title() if word not in whitelist else word for word in words])

Break the problem down to its most basic components. First, you will want some way to capitalize a word. You say in your question that you can't use .title() / .capitalize(), etc... so I will assume .upper() is out as well.

So you want a function like this

def capitalize_word(word):
    # do something to ensure word starts with a capital letter
    # and modify it if it doesn't
    return word

And you are going to want a function that takes a sentence, breaks it into words, and capitalizes each of them (ignoring your exclusion words for now)

def titlecase_sentence(sentence):
    # break the sentence on spaces
    words = sentence.split()
    capitalized_words = []
    # iterate through your words and store the capitalized version of each
    for word in words:
        capitalized_word = capitalize_word(word)
        capitalized_words.append(capitalized_word)
    # join your capitalized words back into a sentence 
    capitalized_sentence = " ".join(capitalized_words)
    return capitalized_sentence

So the two things to figure out are: 1. How does capitalize_word work, and 2. How do we deal with your exclusion words.

The first one is pretty straight forward if you use the ord built-in function. ord returns the ordinal value of an ascii character (its numeric value). The ordinal of 'a' is 97, the ordinal of 'z' is 122. Upper case letters come earlier in the numeric values and are 'A' at 65 and 'Z' at 90. So you can check whether the ord of the first character of word is between 65 and 90, if it isn't then you know you have a lowercase letter. Converting between a lowercase and capital letter is as easy as subtracting 32 from the ordinal and changing it back into an ascii character via the chr built-in. Using this you end up with something like

ord_Z = ord('Z')
ord_A = ord('A')

def capitalize_word(word):
    first_ord = ord(word[0])
    if not (ord_A <= first_ord <= ord_Z):
        word = chr(first_ord - 32) + word[1:]
    return word

Now we just need to handle your special words that do not get capitalized except when beginning a sentence.

I will leave that part as an exercise, but you essentially want to ALWAYS run the first word the capitalize_word function, and conditionally run the remaining words through it. If the current word in the iteration is one of your special words then you simply add the word without calling capitalize_word .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM