-
编写一个程序,交换指定字典的key和value。
例如:dict1={'a':1, 'b':2, 'c':3} --> dict1={1:'a', 2:'b', 3:'c'}
dict1={'a':1, 'b':2, 'c':3} new_dict1 = {} for key, value in dict1.items(): new_dict1.setdefault(value, key) print(new_dict1)
-
编写一个程序,提取指定字符串中所有的字母,然后拼接在一起产生一个新的字符串
例如: 传入'12a&bc12d-+' --> 'abcd'
str1 = '12a&bc12d-+' new_str1 = '' for x in str1: if 'a' <= x <='z' or 'A' <= x <='Z': new_str1 += x print(new_str1)
-
写一个自己的capitalize函数,能够将指定字符串的首字母变成大写字母
例如: 'abc' -> 'Abc' '12asd' --> '12asd'
str1 = 'abc' str2 = '12asd' print(str1.capitalize()) print(str2.capitalize()) str1 = '12444abc' new_str = '' if 'a' <= str1[0] <= 'z': new_str = chr(ord(str1[0]) - 32) + str1[1:] print(new_str) else: print(str1)
-
写程序实现endswith的功能,判断一个字符串是否已指定的字符串结束
例如: 字符串1:'abc231ab' 字符串2:'ab' 函数结果为: True 字符串1:'abc231ab' 字符串2:'ab1' 函数结果为: False
if s1[-len(s2):] == s2: print(True) else: print(False)
-
写程序实现isdigit的功能,判断一个字符串是否是纯数字字符串
例如: '1234921' 结果: True '23函数' 结果: False 'a2390' 结果: False
str1 = '1234921' for x in str1: if not '0' <= x <= '9': print(False) break else: print(True)
-
写程序实现upper的功能,将一个字符串中所有的小写字母变成大写字母
例如: 'abH23好rp1' 结果: 'ABH23好RP1'
str1 = 'abH23好rp1' new_str = '' for x in str1: if not 'a' <= x <= 'z': new_str += x else: new_str += chr(ord(x) - 32) print(new_str)
-
写程序获取指定序列中元素的最大值。如果序列是字典,取字典值的最大值
例如: 序列:[-7, -12, -1, -9] 结果: -1 序列:'abcdpzasdz' 结果: 'z' 序列:{'小明':90, '张三': 76, '路飞':30, '小花': 98} 结果: 98
s1 = [-7, -12, -1, -9] if type(s1) != dict: print(max(s1)) elif type(s1) == dict: print(max(s1.values()))
-
写程序实现replace函数的功能,将指定字符串中指定的旧字符串转换成指定的新字符串
例如: 原字符串: 'how are you? and you?' 旧字符串: 'you' 新字符串:'me' 结果: 'how are me? and me?'
s1 = 'how are you? and you?' s2 = s1.split('you') s3='me'.join(s2) print(s3)
-
写程序实现split的功能,将字符串中指定子串作为切割点对字符串进行切割
例如:原字符串: 'how are you? and you?' 切割点: 'you' 结果: ['how are ', '? and ', '?']
str1 = 'how are you? and you?' result = str1.split('you') print(result)
-
用思维导图总结四大容器:列表、字典、元组、集合
作业2 方法总结
# 1. capitalize()
str1 = 'ab'
print(str1.capitalize())
# 2. center(width, fillchar)
str1 = '快乐风男111223'
print(str1.center(2, 'b'))
# 3. count(str, beg= 0,end=len(string))
string = '快乐风男快乐风男E的很快'
str1 = '快乐风男'
print(str1.count(string))
# 4.bytes.decode()
# bytes.decode([encoding="utf-8"][,errors="strict"])
# 5.encode(encoding='UTF-8',errors='strict')
# 6.endswith(suffix, beg=0, end=len(string))判断字符串是否以指定字符或子字符串结尾,常用于判断文件类型
s = 'hello good boy doiido'
print(s.endswith('o'))
# 7. expandtabs(tabsize=8) 把字符串 string 中的 tab 符号转为空格,tab 符号默认的空格数是 8 。
str = "i love\tpython"
print(str.expandtabs())#默认值为8
print(str.expandtabs(tabsize=8))
print(str.expandtabs())
print(str.expandtabs(2)) #tabsize值为0到7,与tabsize值为8相同
print(str.expandtabs(tabsize=2))
print(str.expandtabs(tabsize=9))
print(str.expandtabs(tabsize=10))
# 8.find(str, beg=0, end=len(string))
info = 'abca'
print(info.find('a'))
print(info.find('a', 1))
# 9.index(str, beg=0, end=len(string))
inf1 = 'abca'
print(inf1.index('a'))
print(inf1.index('a', 1))
# 10.isalnum()
# 如果字符串至少有一个字符并且所有字符都是字母或数字则返 回 True,否则返回 False
str_name = 'HELLOword!'
if str_name.isalnum():
print('True')
else:
print('False')
# 11. isalpha()
# 如果字符串至少有一个字符并且所有字符都是字母或中文字则返回 True, 否则返回 False
str_name = 'HELLOword亚索'
if str_name.isalpha():
print('True')
else:
print('False')
# 12. isdigit()
# 如果字符串只包含数字则返回 True 否则返回 False..
str1 = 'timo123'
print(str1.isdigit())
# 13. islower()
# 如果字符串中包含至少一个区分大小写的字符,并且所有这些(区分大小写的)字符都是小写,则返回 True,否则返回 False
str2 = 'timo'
print(str2.islower())
# 14. isnumeric()
# 如果字符串中只包含数字字符,则返回 True,否则返回 False
str3 = '1234'
print(str3.isnumeric())
# 15. isspace()
# 如果字符串中只包含空白,则返回 True,否则返回 False.
str15 = ' '
print(str15.isspace())
# 16. istitle()
# 检测字符串中所有的单词拼写首字母是否为大写,且其他字母为小写。则返回 True,否则返回 False
a=b='Hello World!'
print(b.istitle())
# 17.isupper()
# 如果字符串中包含至少一个区分大小写的字符,并且所有这些(区分大小写的)字符都是大写,则返回 True,否则返回 False
x='HELLO WORLD!'
print(x.isupper())
# 18.join(seq)
# 以指定字符串作为分隔符,将 seq 中所有的元素(的字符串表示)合并为一个新的字符串
list1 = ['name', 'age', 'gender']
result = '+'.join(list1)
print(result)
# 19.len(string)
# 返回字符串长度
str19 = 'qwer'
print(len(str19))
# 20.ljust(width[, fillchar])
# 返回一个原字符串左对齐,并使用 fillchar 填充至长度 width 的新字符串,fillchar 默认为空格。
str20 = 'qwerdf'
print(str20.ljust(2, '*'))