请用自己目前所学实现字符串大写转小写,小写变大写,并且将字符串变为镜像字符串。例如:’A’变为’Z’,’b’变为’y
示范字符串:
”sdSdsfdAdsdsdfsfdsdASDSDFDSFa”字符串大写变小写 小写变大写,然后最后变为镜像字符串。
Python13期 前来报道!!!
请用自己目前所学实现字符串大写转小写,小写变大写,并且将字符串变为镜像字符串。例如:’A’变为’Z’,’b’变为’y
示范字符串:
”sdSdsfdAdsdsdfsfdsdASDSDFDSFa”字符串大写变小写 小写变大写,然后最后变为镜像字符串。
Python13期 前来报道!!!
#示例一:小写变大写
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)
```python
#实现字符串的大小写互换,并且将字符串镜像输出
# 实现镜像要用到chr函数和ord函数
a="sdSdsfdAdsdsdfsfdsdASDSDFDSFa"
a_swa=a.swapcase()#完成大小写互换
b1=a_swa.replace("a",chr(219-ord("a")))
b2=b1.replace("A",chr(155-ord("A")))
c1=b2.replace("s",chr(219-ord("s")))
c2=c1.replace("S",chr(155-ord("S")))
d1=c2.replace("d",chr(219-ord("d")))
d2=d1.replace("D",chr(155-ord("D")))
e1=d2.replace("f",chr(219-ord("f")))
e2=e1.replace("F",chr(155-ord("F")))
print('''----输出结果如下----
大小写互换:{}
镜像输出: {}'''.format(a_swa,e2))
----输出结果如下----
大小写互换:SDsDSFDaDSDSDFSFDSDasdsdfdsfA
镜像输出: HWhWHUWzWHWHWUHUWHWzhwhwuwhuZ
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
a='sdSdsfdAdsdsdfsfdsdASDSDFDSFa'
print (a.swapcase())
#字符串大小写转换
a='sdSdsfdAdsdsdfsfdsdASDSDFDSFa'
s = ''
for i in a:
if i.islower():
i = chr(219 - ord(i))
s += i
elif i.isupper():
i = chr(155 - ord(i))
s += i
print(s)
#镜像字符串
s="sdSdsfdAdsdsdfsfdsdASDSDFDSFa"
s1=s.swapcase()
print("调整之后的字符串为",s1)
new_s=''
for i in s1:
if i.islower():
i = chr(219-ord(i))
new_s +=i
elif i.isupper():
i=chr(155-ord(i))
new_s += i
print("镜像之后的值:", new_s)
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)
a = 'sdSdsfdAdsdsdfsfdsdASDSDFDSFa'
#字符串a大小写互换
s = a.swapcase()
print('s大小写互换后为:',s)
s.replace('A','Z')
s.replace('b','y')
print(s)
#请用自己目前所学实现字符串大写转小写,小写变大写,并且将字符串变为镜像字符串。例如:’A’变为’Z’,’b’变为’y
str_1="sdSdsfdAdsdsdfsfdsdASDSDFDSFa"
#大小写互换
str_2=str_1.swapcase()
print(str_2)
#变成镜像字符串
str=''
for i in range(len(str_2)):
if str_2[i].isupper():
str+= chr(155-ord(str_2[i]))
if str_2[i].islower():
str+= chr(219-ord(str_2[i]))
print(str)
a='sdSdsfdAdsdsdfsfdsdASDSDFDSFa'
print(a.upper())
print(a.lower())
print(a.swapcase())
b=a.swapcase()
c=''
for i in b:
if i.isupper():
i=chr(155-ord(i))
c +=i
elif i.islower():
i=chr(219-ord(i))
c +=i
print(c)
欢迎来到testingpai.com!
注册 关于