2

OLD MACDONALD: Write a function that capitalizes the first and fourth letters of a name

I'm trying to write this in different ways and I also feel like there's an easier way to do this. Cant, you directly apply it without getting all the other words of a string? Or a split or something?

Here is one of my attempts at trying it another way. I'm trying to do it with a for AND an if statement too. Any help is appreciated.

def old_macdonald(words):
    wordletter = words[0]
    fourthletter = words[3]
    newword = []

    for i,l in enumerate(words):
        if i[0]:
            newword = l.capatalize
            return newword
        if i[3]:
            newword = l.capatalize
    return newword
Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
Kaz Purcell
  • 21
  • 1
  • 1
  • 3
  • Does this answer your question? [change some lowercase letters to uppercase in string](https://stackoverflow.com/questions/8217650/change-some-lowercase-letters-to-uppercase-in-string) – Gino Mempin Jul 09 '20 at 23:53

10 Answers10

4

This is the most legible way IMO

def old_macdonald(name):
    mylist = list(name)
    mylist[0] = mylist[0].upper()
    mylist[3] = mylist[3].upper()
    return ''.join(mylist)
1
def old_macdonald(name):
    letters = list(name)
    for index in range(len(name)):
        if index == 0:
            letters[index] = letters[index].upper()
        elif index == 3:
            letters[index] = letters[index].upper()
    return "".join(letters)

This one worked for me

ggorlen
  • 44,755
  • 7
  • 76
  • 106
Guseyn013
  • 63
  • 9
  • 2
    Welcome to SO and thanks for answering! Why iterate with a loop when you already have the indexes? Lists have random access, so you can say `letters[0] = letters[0].upper()`. – ggorlen Sep 17 '20 at 06:44
1

this code will make first and forth element of the string Capital.

def cap(name):
    first = name[0]
    mid = name[1:3]
    second = name[3]
    rest = name[4:]    
    return first.upper() + mid + second.upper() + rest


cap('kahasdfn')
0
def old_macdonald(s):
    return ''.join([s[:1].upper(), s[1:3], s[3:4].upper(), s[4:]])

# s[:1] -  letter index 0 (first)
# s[1:3] - letters index 1-2
# s[3:4] - letter index 3 (fourth)
# s[4:]  - letters index 4 onward
test_string = "abcdefghijklmnopqr"
print(old_macdonald(test_string )) # AbcDefghijklmnopqr
DarrylG
  • 16,732
  • 2
  • 17
  • 23
0

Here's one way. You do need to split the string into a list of characters to change individual characters, because strings are immutable, and then use join afterward to convert them back into a string.

def old_macdonald(name):
    letters = list(name)
    for letter_to_capitalize in [1, 4]:
        if len(letters) >= letter_to_capitalize:
            letters[letter_to_capitalize - 1] = letters[letter_to_capitalize - 1].upper()

    return "".join(letters)
Tané Tachyon
  • 1,092
  • 8
  • 11
0

A pretty simple and modifiable example.

def old_mcdonald(word):
    indexes = (0,3) #letters i want to cap
    new_word = "".join([v.capitalize() if i in indexes else v for i,v in enumerate(word)])
    return new_word
TheLazyScripter
  • 2,541
  • 1
  • 10
  • 19
0

I like writing it this way:

def old_macdonald(word):
    indices = (0, 4)
    return ''.join(c.capitalize() if index in indices else c for index, c in enumerate(word))

Although it's a little long, so you might prefer writing it more clearly as:

def old_macdonald(word):
    indices = (0, 4)
    new_string = []
    for index, c in enumerate(word):
        new_string.append(c.capitalize() if index in indices else c)
    return ''.join(new_string)
0
def old_macdonald(s):
    w=""
    for i in s:
        t=s.index(i)
        if t==0:
            w=w+i.upper()
        elif(t==3):
            w=w+i.upper()
        else:
            w=w+i
    print(w)
old_macdonald("aman")
  • This answer was reviewed in the [Low Quality Queue](https://stackoverflow.com/help/review-low-quality). Here are some guidelines for [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer). Code only answers are **not considered good answers**, and are likely to be downvoted and/or deleted because they are **less useful** to a community of learners. It's only obvious to you. Explain what it does, and how it's different / **better** than existing answers. – Trenton McKinney Jul 25 '22 at 21:43
0
def old_macdonald(s):
    return s[0:3].capitalize() + s[3:].capitalize()

https://ideone.com/2D0qbZ

Marek R
  • 32,568
  • 6
  • 55
  • 140
0

This one worked

def old_macdonald(name):
    word = ''
    for index,letter in enumerate(name):
        if index == 0 or index == 3:
            letter = name[index].capitalize()
            word += letter
        else:
            word += letter
    return word
  • This answer was reviewed in the [Low Quality Queue](https://stackoverflow.com/help/review-low-quality). Here are some guidelines for [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer). Code only answers are **not considered good answers**, and are likely to be downvoted and/or deleted because they are **less useful** to a community of learners. It's only obvious to you. Explain what it does, and how it's different / **better** than existing answers. – Trenton McKinney Jul 25 '22 at 21:42