请用自己目前所学实现字符串大写转小写,小写变大写,并且将字符串变为镜像字符串。例如:’A’变为’Z’,’b’变为’y
示范字符串:
”sdSdsfdAdsdsdfsfdsdASDSDFDSFa”字符串大写变小写 小写变大写,然后最后变为镜像字符串。
Python13期 前来报道!!!
请用自己目前所学实现字符串大写转小写,小写变大写,并且将字符串变为镜像字符串。例如:’A’变为’Z’,’b’变为’y
示范字符串:
”sdSdsfdAdsdsdfsfdsdASDSDFDSFa”字符串大写变小写 小写变大写,然后最后变为镜像字符串。
Python13期 前来报道!!!
w='sdSdsfdAdsdsdfsfdsdASDSDFDSFa'
w1=w.swapcase()
print('大小写转换后的字符串:',w1)
new_w=''
for i in w:
if i.islower():
i = chr(219-ord(i))
new_w +=i
elif i.isupper():
i = chr(155 - ord(i))
new_w +=i
print('镜像字符串是:',new_w)
#!/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();
运行结果:
a = 'sdSdsfdAdsdsdfsfdsdASDSDFDSFa'
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)
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
欢迎来到testingpai.com!
注册 关于