Evanlee11
Evanlee11

Reputation: 13

Capitalizing first letter in split-append-join operation

I have the following code

def pig(text):

    message = text.split()
    pig_latin = []

    for word in message:
        word = word[1:] + word[0] + 'ay'
        pig_latin.append(word)

    return ' '.join(pig_latin)

def main():
    
    user = str(input("Enter a string: "))
    print(f"Pig latin: {pig(user)}")

Input: Practice makes perfect

Expected Output: Racticepay akesmay erfectpay

My translator is working fine, but I need to capitalize only the first letter of every sentence.

I can't figure out where to place the .capitalize() to get my desired output. I have put it in many locations and no luck so far.

Upvotes: 1

Views: 47

Answers (3)

Evanlee11
Evanlee11

Reputation: 13

Just had to restart VS code now it works lol

Upvotes: 0

Chris
Chris

Reputation: 36581

In addition to what @BrokenBenchmark said, a format string and a generator expression would simplify your code down to a single, readable line of code.

def pig(text):
    return ' '.join(f"{w[1:]}{w[0]}ay" for w in text.split()).capitalize()

Upvotes: 1

BrokenBenchmark
BrokenBenchmark

Reputation: 19242

Capitalizing should be the last thing you do before you return, so put .capitalize() at the return statement.

Change

return ' '.join(pig_latin)

to

return ' '.join(pig_latin).capitalize()

Upvotes: 0

Related Questions