简体   繁体   English

Python如何将单词的第一个字母和后三个字母大写

[英]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从那个字符串我希望结果得到Team Dev FTW

What I tried so far:到目前为止我尝试过的:

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

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)

One way using enumerate :使用enumerate一种方法:

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

Output:输出:

'Team_Dev_FTW'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将 Python 字符串中每个单词的第一个和最后一个字母大写? - How to capitalize first and last letters of each word in a Python string? 如何将 python 中每个单词的首字母大写? - How to capitalize the First letter of each word in python? Python中列表中第一个单词的首字母大写 - Capitalize first letter of the first word in a list in Python 如何大写Python字符串中的行的首尾字母? - How to capitalize first and last letters of line in a Python string? 将Python中的每个单词的首字母大写 - Capitalize first letter of each word in the column Python 如何在 Python 中取单词的前三个字母? - How to take the first three letters of a word in Python? 如何在python中将每个单词的第一个字母大写而不使用.capitalize or.upper or.title - how to capitalize first letter of every word without .capitalize or .upper or .title in python 如何将字符串的第一个和最后一个字母大写? - How to capitalize the first and last letter of a string? 如何反转字符串中的每个单词,并且python中每个单词的第一个字母大写? - How to reverse each word in string and first letter is capitalize of each word in python? Hackerrank Python 挑战:将 Python 中字符串中每个单词的首字母大写 - Hackerrank Python Challenge: Capitalize The First letter of each word in a String in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM