2

in my code I have this line which return a string

return item.title().lower()

item is an object, with a title, and we return that string title but in all lowercase.

How can I do something like

return item.title().lower() but if the words (Maxine, Flora, Lindsey) are in that title, keep them uppercase. All the other words, do lowercase

I can use an if statement but I'm not really sure how to capitalize only specific words.

like

names= ("Maxine", "Mrs. Lichtenstein", "string3")
    if any(s in item.title() for s in names):
        return ???

would something like that work? And what could I return?

rpanai
  • 12,515
  • 2
  • 42
  • 64
user7959439
  • 153
  • 1
  • 9
  • 1
    What have you tried so far based on your own research, and what went wrong with your attempt(s)? Please provide a [mcve] so that we better understand your difficulty – G. Anderson Oct 08 '20 at 22:02
  • 1
    By using an `if` statement where you implement the logic you have just described in the last paragraph. – mkrieger1 Oct 08 '20 at 22:03
  • hey guys I added what I tried – user7959439 Oct 08 '20 at 22:11
  • Probably the simplest is to change the whole string to lowercase as you are doing, then change back the specific words that you want capitalized. Split the lowercase string into "words" (using `item.title().split()`), capitalize them selectively (using `myword.capitalize()`) and then string them back together using `join()`. – BoarGules Oct 08 '20 at 22:15
  • It appears that you’ve misunderstood the use of `.title()`. It does `This To A String`; uppercase the first letter of each word. Then, using `.lower()` converts `a string to all lowercase`. Therefore, `.lower()` is overwriting `.title()`. – S3DEV Oct 08 '20 at 22:17
  • You have your looping backwards. You can do: `return [w.lower() for word in title_str if w not in names]` That assumes `title_str` is a list. Otherwise use `title_str.split()` and then rejoin back into a string. – dawg Oct 08 '20 at 22:35

3 Answers3

1

The following should work (considering that there is no occurence of these words with first character as lowercase (eg maxine) or there is and you want it to upper):

def format(s):
    s=s.lower()
    for i in ('maxine', 'flora', 'lindsey'):
        if i in s:
            s=s[:s.find(i)]+i[0].upper()+i[1:]+s[s.find(i)+len(i):]
    return s

Example:

item.title='My name is Maxine i LIVE IN Flora and I LOVE Lindsey'  
>>> format(item.title)
'my name is Maxine i live in Flora and i love Lindsey'
IoaTzimas
  • 10,538
  • 2
  • 13
  • 30
  • Hey I think this worked just like I wanted but when I have 'mrs. lichtenstein' in the list of names, it converts it to from Mrs. Lichtenstein to Mrs. lichtenstein, only retaining the capitalizion of the Mrs, do you know why? – user7959439 Oct 08 '20 at 22:40
  • This is because the code changes to upper only the first character of these words. Your example Mrs. Lichtenstein is much more difficult to be implemented as there are many cases that must be handled especially if there is a space in between – IoaTzimas Oct 08 '20 at 23:05
0

You're on the right track to use an if statement to check the word against a list of things to keep. Try something like this:

def lowercase_some(words, exclude=[]):
    # List of processed words
    new_words = []
    for word in words:
        if word.lower() in exclude:
            # If the word is in our list of ones to exclude, don't convert
            new_words.append(word)
        else:
            new_words.append(word.lower())

    return new_words

>>> lowercase_some(['TEST', 'WORDS', 'MEXICO'], ['mexico'])
['test', 'words', 'MEXICO']

This can be done in a very Python-ic way with list-comprehension:

def lowercase_some(words, exclude=[]):
    return [word.lower() if word.lower() not in exclude else word for word in words]
Collin Heist
  • 1,962
  • 1
  • 10
  • 21
  • When I try your list comprehension method, I get an error of "Can't assign function to call" Do you know what that means? – user7959439 Oct 08 '20 at 22:29
  • See https://stackoverflow.com/questions/5964927/syntaxerror-cant-assign-to-function-call , are you trying to assign the function `lowercase_some()` to something? The code is working for me. – Collin Heist Oct 08 '20 at 22:46
0

you can use this:

names = ['Maxine', 'Flora', 'Lindsey']
if item.title() in names:
    return item.title()
else:
    return item.title.lower()
Hasti
  • 51
  • 5