1.交换字典建和值的函数
def exchange_key_value(dic:dict):
new_dic={dic[key]:key for key in dic}
print(new_dic)
dict1={'a':1,'b':2,'c':3}
exchange_key_value(dict1)
2.编写一个可以提取字符串中所有字母的函数,并且拼接在一起
#方法一:
def take_letter(str1:str):
new_str1=''
for x in str1:
if 'A'<=x<='Z' or 'a'<=x<='z':
new_str1+=x
print(new_str1)
#方法二:
def take_letter(str1:str):
new_str1=''.join([x for x in str1 if 'A'<=x<='Z' or 'a'<=x<='z'])
print(new_str1)
str2='ingiins2394SMXM'
take_letter(str2)
3.将指定字符串的首字母变为大写字母
def capitalize(string:str):
new_string=str.upper(string[0])+string[1:]
print(new_string)
string1='Ad1356dA'
capitalize(string1)
4.判断字符串中是否是纯数字字符串
def isgigit(string:str):
for x in string:
if not '0'<=x<='9':
print(False)
break
else:
print(True)
string2='15665'
isgigit(string2)