mastaofthepasta
mastaofthepasta

Reputation: 91

Python how to capitalize first letter of a word and last three letters

I am trying to capitalize first letter and last three letter of a matched phrase for example having a string:

test = "TEAM_DEV_FTW_SOMETHING"

from that string I want the result to get Team Dev FTW

What I tried so far:

team = " ".join(map(lambda x: x.capitalize(), test.name.split("_")[:3]))

Upvotes: 0

Views: 95

Answers (2)

Chris
Chris

Reputation: 29732

One way using enumerate:

"_".join([i.upper() if n==2 else i.capitalize() 
          for n, i in enumerate(s.split("_")[:3])])

Output:

'Team_Dev_FTW'

Upvotes: 0

Anton Pomieshchenko
Anton Pomieshchenko

Reputation: 2167

Something like this?:

test = "TEAM_DEV_FTW_SOMETHING"
team = " ".join(map(lambda x: x.capitalize(), test.split("_")[:3]))
team = team[:-3] + team[-3:].upper()
print(team)

Upvotes: 2

Related Questions