Python3 的六个标准数据类型中:
不可变数据(3 个):Number(数字)、String(字符串)、Tuple(元组);
可变数据(3 个):List(列表)、Dictionary(字典)、Set(集合)。
find方法可以在一个较长的字符串中查找子串,他返回子串所在位置的最左端索引,如果没有找到则返回-1
split方法,是一个非常重要的字符串,它是join的逆方法,用来将字符串分割成序列
print(‘1+2+3+4’.split(’+’)) #the result : [‘1’, ‘2’, ‘3’, ‘4’]
1. str = " fgh " 写一个函数,只去掉字符串右侧的空格,左侧的空格保留
def func():
str = " fgh "
str =str.rstrip()
print(str)
func()
2. 输入10个数字到列表中,如果输入的不是数字,则跳过,不存
def s():
l=[]
b=1
while True:
b+=1
if len(l)==10:
break
a=input(‘请输入数字’)
if a.isdigit():
a=int(a)
l.append(a)
else:
continue
print(l)
s()
3. 写一个函数,可以判断一个字符串是否为回文例子qwewq,函数返回true或者false
def func():
a=input(‘请输入字符串:’)
if a == a[::-1]:
return True
else:
return False
print(func())
4. 请手写一个函数,可以打印出 I’m “ok” it’s your’s 注意必须是原样输出
def s():
print(“I’m ‘ok’ it’s your’s”)
s()
5. str2 = “This is the voa special English,health,report” 写一个函数,统计字符串中单词出现的个数,注意是单词而不是字母
def func():
str2 = “This is the voa special English,health,report”
str2 = str2.replace(",","")
print(str2)
l=str2.split()
print(len(l))
func()
6. My_str = ‘11sdsfsdf45sfxcv67qwe_9’ 手写一个函数,计算出字符串中所有数字的和
def func():
l=[]
My_str = ‘11sdsfsdf45sfxcv67qwe_9’
for i in My_str:
if i isdigit():
l.append()(int(i))
print(l)
print(sum(l))
func()
7. s = ‘test’ 写一个函数,能将字符串中的网址提取出来,即提取出www.test.com
def func():
l =s.split(’"’)
print(l[1])
func()
8. str = “卡巴斯基#杀毒软件#免费版#俄罗斯#” 手写一个函数,将该字符串解析为[‘卡巴斯基’, ‘杀毒软件’, ‘免费版’, ‘俄罗斯’]
def func():
str = “卡巴斯基#杀毒软件#免费版#俄罗斯#”
l=str.split(’#’)
del l[-1]
print(l)
func()