-
Python13 期字符串大考核!!!
2020-07-24 10:55a = "sdSdsfdAdsdsdfsfdsdASDSDFDSFa" # 大小写互换 new_a = a.swapcase() # 镜像字符串 str_image = ""# 镜像后的字符串 str_1 = "abcdefghijklmnopqrstuvwxyz" str_2 = str_1[::-1] # 小写字母对应镜像字符串 for i in new_a: if i.islower(): str_image += str_2[str_1.find(i)] else: str_image += str_2[str_1.find(i.lower())].upper() print("""大小写互换后:{} 镜像字符串为:{}""".format(new_a,str_image))
-
Python13 期字符串大考核!!!
2020-07-24 10:55a = 'sdSdsfdAdsdsdfsfdsdASDSDFDSFa'
#大小写转换
s = a.swapcase()
print(s)#A变为Z
b = s.replace('A','Z')
print(b)#d变为y
b = s.replace('d','y')
print(b) -
Python13 期字符串大考核!!!
2020-07-24 10:55#定义一个字符串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)) -
Python13 期字符串大考核!!!
2020-07-24 10:55#!/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();
运行结果: -
Python13 期字符串大考核!!!
2020-07-24 10:55a="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)