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 
1

10 Answers 10

5

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) 
Sign up to request clarification or add additional context in comments.

Comments

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

1 Comment

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().
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') 

Comments

1

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 

1 Comment

This answer was reviewed in the Low Quality Queue. Here are some guidelines for How do I write a good 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.
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) 

Comments

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 

Comments

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 

Comments

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) 

1 Comment

Oops, it looks like TheLazyScripter already said almost the exact same thing
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") 

1 Comment

This answer was reviewed in the Low Quality Queue. Here are some guidelines for How do I write a good 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.
0
def old_macdonald(s): return s[0:3].capitalize() + s[3:].capitalize() 

https://ideone.com/2D0qbZ

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.