【问题标题】:Turn upper case to lowercase, lowercase to upper, add +5 to all numbers modulo 10大写转小写,小写转大写,所有数字模10加+5
【发布时间】:2022-11-11 20:54:02
【问题描述】:

我需要将大写转换为小写,将小写转换为大写,并且我需要将每个数字 +5 模 10 相加。它不起作用,请您帮帮我吗? 句子是“Hello World, 521”,输出应该是“hELLO wORLD, 076”。我需要使用函数定义。

我试过这个:

def fc1 (string):
    if string.upper == True:
        return string.lower
    else:
        return string.upper
    if string.isdigit == True:
        return ((string + 5 ) % 10)


fc1 ("Hello World, 521")

【问题讨论】:

    标签: python


    【解决方案1】:

    使用str.swapcase

    ''.join(str((int(i)+5)%10) if i.isdigit() else i.swapcase() for i in 'hello world 521')
    

    【讨论】:

      猜你喜欢
      • 2011-01-16
      • 1970-01-01
      • 2022-10-13
      • 2016-01-25
      • 1970-01-01
      • 2012-11-19
      • 2014-02-19
      • 1970-01-01
      • 2013-02-04
      相关资源
      最近更新 更多