小编Mik*_*sov的帖子

大写功能的较短代码

我在这里解决了这个问题https://www.hackerrank.com/challenges/capitalize

描述:给你一个字符串.你的任务是利用它的每个词.总之,只有第一个字符大写.大写时的示例12abc仍然是12abc - 因为这个'title'不适用于像'1 w 2 r 3g'这样的字符串.我需要检查数字和小写字母的组合.这是我的代码:

def capitalize(string):
    result = list (string.title())
    for index in range (len (string)-1):
      if string[index].isdigit () and string[index+1].islower ():
        result[index+1] = result[index+1].lower()
    result = ''.join([char for char in result])
    return (result)
Run Code Online (Sandbox Code Playgroud)

但是这段代码太麻烦了.有人可以帮助更优雅的pythonic决定吗?谢谢!

python python-3.x

2
推荐指数
1
解决办法
661
查看次数

标签 统计

python ×1

python-3.x ×1