Python13 期字符串大考核!!!

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

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

Python13期 前来报道!!!

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

    image.png

  • phoenix

    a="sdSdsfdAdsdsdfsfdsdASDSDFDSFa"
    print(a.swapcase())
    b=a.swapcase()
    new_str=''

    for i in b:
    if i.islower():
    i=chr(219-ord(i))
    new_str += i
    elif i.isupper():
    i=chr(155-ord(i))
    new_str +=i
    print( new_str)

  • phoenix

    image.png

  • chenfeng1987

    image.png

  • huahua

    这。。。不对吧?

  • huahua

    这。。。是公开课的答案吧?我要你用今天学的字符串来做哦!

  • 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

  • qizai

    #定义一个字符串a
    a="sdSdsfdAdsdsdfsfdsdASDSDFDSFa"
    #字符串a大写
    a_up=a.upper()
    #字符串a小写
    a_low=a.lower()
    #字符串中的A替换为Z
    a_z=a.replace("A","Z")
    #字符串中没有b,本行结果不变
    a_zy=a_z.replace("b","y")
    #格式化输出结果
    print("""
    ----字符串作业---
    {0}大写结果{1}
    {0}小写结果{2}
    {0}镜像字符串’A’变为’Z’,’b’变为’y’ 结果{3}
    """.format(a,a_up,a_low,a_zy))

  • qizai

    image.png

  • xiaochen_python13

    image.png

  • 1310

    image.png
    image.png

  • 1310

    乔木

  • arong

    交作业~~!还是用到了for循环,如果不用循环的话,除了将出现过的元素手动replace()替换之外,还没有想到更好的方法
    image.png

  • hlg

    image.png

  • grq

    image.png

  • 13581628167

    image.png

  • changhen

    image.png

  • mmy

    image.png

  • meng

    a = 'sdSdsfdAdsdsdfsfdsdASDSDFDSFa'
    #大小写转换
    s = a.swapcase()
    print(s)

    #A变为Z
    b = s.replace('A','Z')
    print(b)

    #d变为y
    b = s.replace('d','y')
    print(b)

  • 17701306874

    image.png

  • pitt

    image.png

  • xiaoma

    作业image.png

  • xiaocc

    image.png

  • lipeng

    image.png

    image.png

  • lnj

    image.png

  • Polaris

    image.png

  • da_haha

    image.png

  • Polaris

    image.png

  • 18270811807

    image.png

  • panshi

    image.png

请输入回帖内容 ...