Python13 期字符串大考核!!!

本贴最后更新于 1544 天前,其中的信息可能已经时移世异

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

Python13期 前来报道!!!

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

    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

  • eason

    a = 'sdSdsfdAdsdsdfsfdsdASDSDFDSFa'

    b=a.swapcase()

    print(a.swapcase())

    first=''
    for i in a:
    if i.islower():
    first = first + i.upper()
    else:
    first = first + i.lower()

    last=''
    for i in a:
    if i.islower():
    last=last+chr(219 - ord(i))
    else:
    last = last + chr(155 - ord(i))
    print (first)
    print(last)

  • Limei

    s="sdSdsfdAdsdsdfsfdsdASDSDFDSFa"
    print(s.upper())
    print(s.lower())
    print(s.swapcase())

    print("替换前:",s)
    s_1=s.replace('s','h')
    s_2=s_1.replace('S','H')
    s_3=s_2.replace('f','u')
    s_4=s_3.replace('A','Z')
    s_5=s_4.replace('D','W')
    s_6=s_5.replace('F','U')
    s_7=s_6.replace('d','w')
    s_8=s_7.replace('a','z')
    print("替换后:",s_8)

  • 查看更多回帖