【问题标题】:Upper case a full string without using .upper function? PYTHON大写一个完整的字符串而不使用 .upper 函数? PYTHON
【发布时间】:2015-01-12 08:08:45
【问题描述】:

我想知道如何在不使用任何涉及 .upper() 的函数的情况下完全大写字符串?有没有办法用 ord() 函数大写?我正在进行一项练习,我必须避免使用 .upper 函数。

【问题讨论】:

  • 在 IDLE 中尝试ord('a') - ord('A')。

标签: function case uppercase


【解决方案1】:
ord("A") --> 65                  ord("B") --> 66            ...
ord("a") --> 97                  ord("b") --> 98            ...

所以每当你想在不使用upper()函数的情况下将小写字符变为大写时,你可以使用ord()函数找到小写的ASCII值,减去32(这是大写和小写之间的差异)然后使用chr() 函数将其转换回字符。

【讨论】:

    【解决方案2】:
    print ('Case changer(Capital to small ONLY)')
    line1 = input ('Enter here: ')
    g = ""
    for ch in line1:
        if ord (ch) >= 65 and ord(ch) <= 90:
            x = ord(ch) + 32
            y = chr (x)
            g = g + y
    print(g)
    

    【讨论】:

    • 虽然此代码可能有助于解决问题,但提供有关 why 和/或 如何 回答问题的附加上下文将显着改善长期价值。请edit你的答案添加一些解释。
    猜你喜欢
    • 1970-01-01
    • 2022-10-05
    • 1970-01-01
    • 1970-01-01
    • 2019-07-03
    • 2016-05-22
    • 2021-12-17
    • 2016-02-08
    • 2016-12-11
    相关资源
    最近更新 更多