目录
一、字符串有关操作函数
1.capitalize()
将字符串的第一个字符转换为大写。
words = "hello world hello world"
print(words.capitalize())
输出:
Hello world hello world
2.title()
返回“标题化”的字符串,即将所有单词的首字母大写,其余字母均为小写。
words = "hello world hello world"
print(words.title())
输出:
Hello World Hello World
3.upper()
将字符串中的小写字母转换为大写字母。
words = "Return a copy of the string with all the cased characters converted to uppercase. "
print(words.upper())
输出:
RETURN A COPY OF THE STRING WITH ALL THE CASED CHARACTERS CONVERTED TO UPPERCASE.
4.lower
将字符串中的大写字母转换为小写字母。
words = "RETURN A COPY OF THE STRING WITH ALL THE CASED CHARACTERS CONVERTED TO LOWERCASE."
print(words.lower())
输出:
return a copy of the string with all the cased characters converted to lowercase.
5.isupper()
判断字符串中是否所有字母都大写,返回布尔值。
"isupper".isupper()
False
"Isupper".isupper()
False
"ISUPPER".isupper()
True
6.islower()
判断字符串中是否所有字母都小写,返回布尔值。
"islower".islower()
True
"Islower".islower()
False
"ISLOWER".islower()
False
7.count()
返回字符串中包含子字符串的个数,本函数包含三个参数,count(查找的字符串,起始位置(可选),结束位置(可选)),指定起始位置和结束位置类似切片,如果未查找到返回-1。
words = "hello world"
print(words.count("l"))
输出:
3
words = "hello world"
print(words.count("lo",0,5))
输出:
1
8.find()
返回子字符串在字符串中的最低索引,本函数包含三个参数,find(查找的字符串,起始位置(可选),结束位置(可选)),指定起始位置和结束位置类似切片,如果未查找到返回-1。
words = "hello world"
print(words.find("lo",0,5))
输出:
3
words = "hello world"
print(words.find("py"))
输出:
-1
9.index()
返回子字符串在字符串中的最低索引,本函数包含三个参数,find(查找的字符串,起始位置(可选),结束位置(可选)),如果未找到程序会报错。
index函数和find函数很相似,区别是如果未包含子字符串,find函数返回-1,index函数抛出异常。
words = "hello world"
print(words.index("py"))
输出:
Traceback (most recent call last):
File "/Users/binhu/PycharmProjects/learnpython/temp.py", line 2, in <module>
print(words.index("py"))
ValueError: substring not found
Process finished with exit code 1
10.split()
通过指定分隔符对字符串进行分割,返回的值为分割后的字符串列表。本函数包含两个参数,split(分隔符,maxsplit = 分割次数(可选))。
"hello world world".split() #拆分单词
输出:
['hello', 'world', 'world']
"1,2,,3,4".split(',')
输出:
['1', '2', '', '3', '4']
words = "a,b,c,d,e,f,g"
print(words.split(","))
输出:
['a', 'b', 'c', 'd', 'e', 'f', 'g']
print(words.split(',',maxsplit=3)) # 指定拆分次数
输出:
['a', 'b', 'c', 'd,e,f,g']
如果不指定maxsplit的值,将按最大可分割次数进行分割。
11.splitlines()。
返回字符串的行列表,在行边界处中断。本函数有一个参数。splitlines(keepends = 通用换行符)
'ab c\n\nde fg\rkl\r\n'.splitlines()
输出:
['ab c', '', 'de fg', 'kl']
'ab c\n\nde fg\rkl\r\n'.splitlines(keepends=True)
输出:
['ab c\n', '\n', 'de fg\r', 'kl\r\n']
words = '''\
Thoughts of you dance through my mind。
Knowing,it is just a matter of time。
Wondering。。。will u ever be mine?
You are in my dreams, night。。。 and sometimes。。。 day。
The thoughts seem to never fade away。\
'''
print(words.splitlines())
输出:
['Thoughts of you dance through my mind。', 'Knowing,it is just a matter of time。', 'Wondering。。。will u ever be mine?', 'You are in my dreams, night。。。 and sometimes。。。 day。', 'The thoughts seem to never fade away。']
12.startwith()
用于检查字符串是否以指定字符串开头,如果是,返回True,否则返回False。该函数包含三个参数,startwith(检测的字符串,起始位置(可选),结束位置(可选))
words = "apply"
print(words.startswith("ap"))
输出:
True
words = "apply apple abandon apollo append"
for i in words.split():
print(i.startswith("ap"))
输出:
True
True
False
True
True
13.endwith()
用于检查字符串是否以指定字符串结尾,如果是,返回True,否则返回False。该函数包含三个参数,endwith(检测的字符串,起始位置(可选),结束位置(可选))
14.strip()
删除字符串头尾指定的字符(默认为删除空格)。
words = 'hello world '
print(words.strip())
输出:
hello world
words = '&&hello world '
print(words.strip('&'))
输出:
hello world
words = '&&hello &world '
print(words.strip('&'))
输出:
hello &world
15.lstrip()
删除字符串左边指定的字符(默认为删除空格)。
16.rstrip()
删除字符串右边指定的字符(默认为删除空格)。
17.replace()
把字符串中的旧字符串替换为新字符串。该函数包含三个,replace(旧字符串,新字符串,替换的次数(可选))
words = 'apple'
print(words.replace('e','y'))
输出:
apply
words = '&&hello &world '
print(words.replace('&',''))
输出:
hello world
二、练习:字符串大冒险
1.找到字母b。puzzle中的字符串由20行*20列的字符串组成,本任务要求编写一段程序,找出藏在字符串中的字母b,并按如下样式输出找到的结果:第x行第y列的值为'b'
puzzle ='''\
pppppppppppppppppppp
pppppppppppppppppppp
pppppppppppppppppppp
pppppppppppppppppppp
pppppppppppppppppppp
pppppppppppppppppppp
pppppppppppppppppppp
pppppppppppppppppppp
pppppbpppppppppppppp
pppppppppbpppppppppp
pppppppppppppppppppp
pppppppppppppppppppp
pppppppppppppppppppp
pppppppppppppppppppp
pppppppppppppppppppp
pppppppppppppppppppp
ppppppppppppppbppppp
pppppppppppppppppppp
pppppppppppppppppppp
pppppppppppppppppppp\
'''
2.清洗电话号码。puzzle列表中共有100条电话数据,由于录入失误,有的电话号码的前后参杂了'*','!'和空格,甚至在电话号码的内部还夹杂了'$',你的任务是将这些污染字符删除掉,输出正确的电话号码。
puzzle = [' 14281898021 ', '185$75586045', '15137941561', '16809394802', ' 17337986394 ', '18964818314', '10266307$280', '19665705738', '12037843134', '18033642678', '17772169$267', ' 16316268264 ', '11956836479', '1$2467813477', '13661770222', '18808495468', '13091635536!', '*14876077313', '15613957370', ' 11660566007 ', '13557998389', ' 19276354498 ', '163$60259619', ' 12041704119 ', '1588530$4759', '16967977863', '*13110057429', '13064$495058', ' 18226181297 ', '10034741157!', '12556926223', '13426799862!', '*15455403772', '10347891163!', '12193568165!', '*12075389065', '*13628425891', '14680194416', '15771153865', '13758311068', '*19613052274', '12456173$802', '196858609$48', ' 16492432809 ', '18553728389!', '14$794919603', '13655210843!', '16374613839!', '12021952809!', '19995179771', '10355238209', '12897435780', ' 11926709244 ', '*11952404140', '16629470954', '14523856410', '16487869287!', '*18703692513', '*14904198425', '19691403311', '10510080321!', '*17414312045', '16912162507', '19651733375!', ' 14057909229 ', '11601246$615', '14564468069', '16427509370', '*17705856381', ' 11596209468 ', '15291858314', '*10909146313', ' 12774223618 ', ' 16098026687 ', '141721407$54', '*12876852245', '195774462$93', '10454371262', '14854409775', '*12790301720', '11596542421', '*14216949982', '18302286534!', '*19241312859', '*17254850403', '16975867127', '19337661495', '15246495742', '18447621447', '11871791595', ' 19658912345 ', '19831450685', '19972899414', '14609540267!', '18888031083', '10208901454', '11978$304933', '17661714374!', '17497180265', ' 15967997558 ']
3.(附加题)编写一个程序,请输入星期的第一个字母,用来判断是星期几,如果第一个字母一样,例如用户输入T,则提示用户“你想要的是Tuesday吗?”,等待用户输入yes或者no,如果用户输入no,则提示用户“你想要的是Thursday吗?”,如果用户输入yes,则请用户输入星期的第二个字母,以此类推,直到用户输入的所有字母加起来刚好等于Thursday,输出“这是你想要的Thursday”。