Python13 期字符串大考核!!!

请用自己目前所学实现字符串大写转小写,小写变大写,并且将字符串变为镜像字符串。例如:’A’变为’Z’,’b’变为’y
示范字符串:
”sdSdsfdAdsdsdfsfdsdASDSDFDSFa”字符串大写变小写 小写变大写,然后最后变为镜像字符串。

Python13 期 前来报道!!!

107 回帖
请输入回帖内容 ...
  • Katt

    image.png

  • 其他回帖
  • sophia

    # 示例一:小写变大写
    msg_1 = 'my name is sophia'
    print('小写变大写后是:'+msg_1.upper())

    # 示例二:大写变小写
    msg_2 = 'MY NAME IS SOPHIA'
    print('大写变大写后是:'+msg_2.lower())

    # 示例三:大写变小写,小写变大写,生成镜像字符串;利用字符串的内置方法来解决
    def change_msg_3(s):
    s = s.swapcase()# 互换大小写
    in_tab = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
    out_tab = 'ZYXWVUTSRQPONMLKJIHGFEDCBAzyxwvutsrqponmlkjihgfedcba'
    reflect = s.maketrans(in_tab, out_tab)# 建立翻译表
    s = s.translate(reflect)# 根据翻译表进行翻译
    return s
    if name == 'main':
    s = 'MynameisSOPHIA'
    new_s = change_msg_3(s)
    print("转换之前的字符串:", s)
    print("转换之后的字符串:", new_s)

    image.png

  • 88888888
    #!/usr/bin/python3
    # -*- coding: utf-8 -*-
    # @Time     :2018/12/1 11:52
    # @Author   :Yosef
    # E-mail    :wurz529@foxmail.com
    # File      :homework.py
    # Software  :PyCharm Community Edition
    
    a = "sdSdsfdAdsdsdfsfdsdASDSDFDSFa"
    """
    1. 字符串大写变小写 
    2. 小写变大写
    """
    b=''
    for i in a:
        if i.islower():
            b += i.upper()
        else:
            b += i.lower()
    print(b)        # 完成大小写互换
    '''
    3. 转换镜像。 a-z A-Z
    '''
    lower = "abcdefghijklmnopqrstuvwxyz"
    upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    
    mirror=""
    for i in b:
        if lower.find(i)!=-1:
            index = lower.find(i)
            mirror += lower[25-index]
        elif upper.find(i)!=-1:
            index = upper.find(i)
            mirror += upper[25-index]
    print(mirror)
    

    关于第一部分的大小写转换也可以使用 swapcase();
    运行结果:

    image.png

  • JUNE

    c='sdSdsfdAdsdsdfsfdsdASDSDFDSFa'
    print(c.swapcase())
    new_c=''
    for i in c:
    if i.islower():
    i=chr(219-ord(i))
    new_c += i
    elif i.upper():
    i=chr(155-ord(i))
    new_c += i
    print(new_c)

    结果:
    SDsDSFDaDSDSDFSFDSDasdsdfdsfA
    hwHwhuwZwhwhwuhuwhwZHWHWUWHUz

  • 查看更多回帖