Python基础练习题之5——日常打卡练习

本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明
部分答案依旧参考原博主https://blog.csdn.net/xufive/article/details/102993541题库也来自于https://blog.csdn.net/xufive/article/details/102993538写着标准答案的批注是觉得原作者的答案很不错很值得参考
————————————————
版权声明:本文为CSDN博主「gttar_xing」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/gttar_xing/article/details/103498654

#####################
### 12th,December,2019  writed by Xingfu  Tuple
#####################
import numpy as np

### 44 返回字符串'abCDEfg'的全部大写,全部小写和大小写互换形式
cha = 'abCDEfg'
cha1 = cha.lower()
cha2 = cha.upper()
# cha3 = cha.title()
cha3 = cha.capitalize()
print("小写",cha1)
print("大写",cha2)
print("首字母大写",cha3)
## 大小写互换:
cha4 = cha.swapcase()
print('大小写互换:',cha4) ## outcome: 大小写互换: ABcdeFG

### 45 判断字符串'AbCDEfg'是否全部大写,是否全部小写,是否首字母大写
s = 'Love'
print(s.istitle()) ## 发现这个只针对于正常拼写的英文字母
print(s.isupper())
print(s.islower())

### 46 返回字符串'this is python'首字母大写以及字符串内每个单词首字母大写形式
s1 = 'this is python'
## 首字母大写
s2 = s1.capitalize()
print('s2',s2)
## 每个单词首字母大写
s3 = s1.title()
print('s3',s3)

### 47 判断字符串 'this is python'是否以'this'开头,又是否以'python'结尾
s1 = 'this is python'
out1 = s1.startswith('this')
out2 = s1.endswith('python')
print('out1',out1)
print('out2',out2)

### 48 返回字符串 'this is python'中'is'出现的次数
s1 = 'this is python'
print(s1.count('is')) ## outcome : 2

### 49 返回字符串 'this is python'中'is'首次出现的位置和最后一次出现的位置
s1 = 'this is python'
print(s1.find('is')) ## 首次出现的索引包括空格哦
print(s1.rfind('is')) ## 最后一次出现的索引
print(s1.find('m')) ## 若未找到返回值为-1

### 50 将字符串'this is python'切片成三个单词
word = 'this is python'
print(word.split())  ## outcome: ['this', 'is', 'python']

### 51 返回字符串"https://blog.csdn.net/gttar_xing"按路径分隔符切片的结果
road = 'https://blog.csdn.net/gttar_xing'
road_split = road.split(sep='/')
print(road_split)

### 52 将字符串'2.72,5,7,3.14'按照半角逗号切片后,再将各个元素转换成浮点型或整型
st = '2.72,5,7,3.14'
[float(i) for i in st.split(sep=',')]
print('st',st)
## 标准参考答案:
## >>> s = '2.72, 5, 7, 3.14'
## [float(item) if '.'in item else int(item) for item in s.split(',')]
st1 = [float(item) if '.' in item else int(item) for item in st.split(',')]
print('st1',st1)

### 53 判断字符串'this is python 2.72  5  7 3.14'是否全为数字,是否全为字母,是否全为字母数字,是否全为ASCII码
sti = 'this is python 2'
print(sti.isalnum())
print(sti.isalpha())
print(sti.isdigit())
print(sti.isascii())

### 54 将字符串'There is python'中的'is'替换成 are
wor = 'There is python'
newword = wor.replace('is','are')
print('newword:',newword)


### 55 清除字符串'\t Python \n'左侧'右侧'以及左右两侧的空白字符
s_t_r = '\t Python \n'
v1 = s_t_r.lstrip()
v2 = s_t_r.rstrip()
v3 = s_t_r.strip()
print('左侧',v1)
print('右侧',v2)
print('空白',v3)

### 56 将三个全英文字符串('ok','hello','thank you')分行打印,实现左对齐,右对齐和居中效果
ln = ['ok','hello','thank you']
len_max = max([len(i) for i in ln]) ## 取最长的字符串长度
## 左对齐:
for i in ln:
    print('%s'%i.ljust(len_max))
## 右对齐:
for s in ln:
    print('%s'%s.rjust(len_max))
## 居中对齐:
for p in ln:
    print('%s'%p.center(len_max))

### 57 将三个字符串('你好,','我是','Miaoxingren','很高兴认识你')分行打印,实现左对齐,右对齐和居中效果
### 标准答案:
## >>> a = ['Hello, 我是David', 'OK, 好', '很高兴认识你']
###>>> a_len = [len(item) for item in a] # 各字符串长度
###>>> a_len_gbk = [len(item.encode('gbk')) for item in a] # 各字符串gbk编码的字节码长度
###>>> c_num = [a-b for a,b in zip(a_len_gbk, a_len)] # 各字符串包含的中文符号个数
###>>> len_max = max(a_len_gbk) # 最大字符串占位长度
###>>> for s, c in zip(a, c_num):
###	print('"%s"'%s.ljust(len_max-c))

xf = ['你好,','我是','Miaoxingren','很高兴认识你']
len_xf = [len(i) for i in xf] # outcome:[3, 2, 11, 6]
len_gbk = [len(i.encode('gbk')) for i in xf] # outcome:[5, 4, 11, 12]
chinese_num = [a-b for a,b in zip(len_gbk,len_xf)]
len_xf_max = max(len_gbk)
for s,c in zip(xf,chinese_num):
    print('%s'%s.ljust(len_xf_max-c))

 

你可能感兴趣的:(Python,基础练习题)