Python Forum
Optimizing string capitalization challenge
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Optimizing string capitalization challenge
#1
Here is the Udemy instructor's challenge:

Quote:Write a function that capitalizes the first and fourth letters of a name
old_macdonald('macdonald') --> MacDonald

Here is my solution:

def old_macdonald(name):
    first_cap = name[0].capitalize() + name[1:3]
    second_cap = name[3].capitalize() + name[4:]   
    together = first_cap + second_cap
    return together
This produces the desired output: MacDonald but I was wondering how you people would perhaps optimize this or re-write it in a more Pythonic way?

For my future reference, the course material I am working is publicly available here: Pierian-Data/Complete-Python-3-Bootcamp. The specific sub- module I am working on is: 03-Methods and Functions/03-Function Practice Exercises.ipynb
Reply


Messages In This Thread
Optimizing string capitalization challenge - by Drone4four - Dec-30-2018, 02:24 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Team Chooser Challenge kanchiongspider 3 2,366 Jun-02-2020, 04:02 AM
Last Post: kanchiongspider
  Meal cost challenge Emekadavid 3 2,858 Jun-01-2020, 02:01 PM
Last Post: Emekadavid
  Appending To Files Challenge erfanakbari1 3 2,936 Mar-27-2019, 07:55 AM
Last Post: perfringo
  Problem with a basic if challenge erfanakbari1 2 1,992 Oct-12-2018, 08:04 AM
Last Post: erfanakbari1
  trying an online challenge tozqo 8 5,960 Jun-21-2017, 07:07 AM
Last Post: Kebap

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020