字符串
1、字符串
单引号、双引号、三引号括起来的文本
e.g.: ‘1a掌声’ “D:\Linux苗”
如果文本中包含引号,我们交叉使用
2、转义字符
""来表示,可以改变原来的含义
如果不让“\”表示转义,在字符串前面加字符r
转义字符 | 含义 |
---|---|
\n | 换行 |
\ | 反斜杠符号 |
\b | 退格键(Backspace) |
" | 双引号本身 |
’ | 单引号本身 |
\r | 回车 |
\t | 横向制表位tab |
text1='北京\n西安\n上海'
print(text1)
text2='北京\\西安\\上海'
print(text2)
text3="D:\Linux苗\miao"
print(text3)
text4=r"D:\ninux苗\miao"
print(text4)
print("let's go go!")
print('let\'s go go!')
北京
西安
上海
北京\西安\上海
D:\Linux苗\miao
D:\ninux苗\miao
let's go go!
let's go go!
text1=r'北京\n"西安"\n上海'
print(text1)
北京\n"西安"\n上海
3、访问字符串中的值
1)字符串在内存中的存储方式
每一个字符对应一个编号,编号是从0开始,依次增加
school="西安科技大学"
print(school[2])
print(school[3])
科
技
2)字符串切片的使用格式
格式:变量名[起始:结束:步长]
说明:包含起始,不包含结束(包前不包后)
print(school[0:4])
print(school[2:]) #结束位置省略,表示到字符串尾
print(school[:4]) #起始位置省略,表示从头开始
print(school[::2]) #起始和结束位置都省略,表示从头到尾
print(school[::-1]) #步长为负数,表示从后往前
print(school[::-2])
西安科技
科技大学
西安科技
西科大
学大技科安西
学技安
函数(参数):完成一个特定功能的程序段
使用时直接调用:函数名(参数)
方法:完成特定功能的一个程序段
使用时:对象.方法()
4、字符串常用函数/方法
1) find
功能:用于检测一个字符串中是否包含另一个字符串,如果包含,返回字符串开始的索引编号,如果不包含返回-1
格式:str.find(子字符串[,start,end])
start如果省略:默认为0
end如果省略:默认到尾
school="西安科技大学"
print(school.find("科技")) #start和end都省略表示从头到尾
print(school.find("科技",4))
2
-1
2) index
功能:用于检测一个字符串中是否包含另一个字符串,如果包含,返回字符串开始的索引编号,如果不包含返回-1
格式:str.index(子字符串[,start,end])
start:默认为0
end:表示字符串的长度
school="西安科技大学"
print(school.index("科技")) #start和end都省略表示从头到尾
2
3) count
功能:用来统计字符串中出现子串的次数
格式:str.count(子字符串[,start,end])
start:默认为0
end:表示字符串的长度
school="西安科技大学科技科技科技科技科技"
print(school.count("科技"))
6
4) replace
功能:用新的字符串替换旧的字符串
格式:str.replace(old,new[,count])
count:替换次数
school="西安大学西安西安西安西安西安"
print(school.replace("西安","1"))
print(school.replace("西安","1",3))
1大学11111
1大学11西安西安西安
5) split
功能:用指定分隔符对字符串进行切片
格式:str.split(参数)
school="西安科技科技科技科技科技科技科技科技科技"
print(school.split())
print(school.split('科',3))
['西安科技科技科技科技科技科技科技科技科技']
['西安', '技', '技', '技科技科技科技科技科技科技']
str='i am python'
str_1=str.split()
print(str_1)
for i in str_1:
print(i)
print(str_1[0],str_1[1],str_1[2])
['i', 'am', 'python']
i
am
python
i am python
6) capitalize()
功能:将字符串的第一个字母变成大写
print("python".capitalize())
Python
7) title()
功能:字符串中首字母大写
str="i am python!"
print(str.title())
I Am Python!
8) startswith()
功能:用于检查字符串是否是指定的字符开头
格式:对象.startswith(‘字符串’)
str="i am python!"
print(str.startswith('i'))
print(str.startswith('hello'))
True
False
9) endswith()
功能:检查字符串是否是以指定字符串结尾
格式:对象.endswith(‘字符串’)
str_2="i am python"
print(str_2.endswith('python'))
print(str_2.endswith('PYTHON'))#区分大小写
True
False
10) upper()
功能:将字符串中小写字母转换为大写字母
格式:对象.upper()
old_str='hello world itheima and ithemaAPP'
new_str=old_str.upper()
print(new_str)
HELLO WORLD ITHEIMA AND ITHEMAAPP
11) ljust
功能:返回一个原字符串左对齐,并使用空格填充指定长度的新字符串、
格式:str.ljust(width[,fillchar]) #width:字符串长度 fillchar:填充字符,默认为空格
str_example="Runoob example ... wow!!!"
print(str_example.ljust(50,'*'))
Runoob example ... wow!!!*************************
Runoob example ... wow!!!
12) rjust
功能:返回一个原字符串右对齐,并使用空格填充指定长度的新字符串、
格式:str.rjust(width[,fillchar]) #width:字符串长度 fillchar:填充字符,默认为空格
old_str='hello world itheima and ithemaAPP'
new_str=old_str.rjust(50)
print(new_str)
hello world itheima and ithemaAPP
13) center
功能:返回一个宽度为wide、原字符串居中、以fillchar(默认为空格)填充左右两边
格式:str.center(width[,fillchar])
old_str='hello world itheima and ithemaAPP'
new_str=old_str.center(50)
print(new_str)
hello world itheima and ithemaAPP
14) lstrip
功能:用于截掉字符串左边的空格或者指定字符,返回的是一个新字符串
格式:str.lstrip([chars])
old_str_one=' hello world itheima and ithemaAPP'
old_str_two=' hello world itheima and ithemaAPP'
new_str_one=old_str_one.lstrip()
new_str_two=old_str_one.lstrip()
print(new_str_one)
print(new_str_two)
hello world itheima and ithemaAPP
hello world itheima and ithemaAPP
15) rstrip
功能:用于删除字符串右边的空格或者指定字符,返回的是一个新字符串
格式:str.rstrip([chars])
old_str=' hello world itheima and ithemaAPP '
new_str=old_str.rstrip()
print(new_str)
hello world itheima and ithemaAPP
16) strip
功能:用于移除字符串头尾的指定字符,返回的是一个新字符串
格式:str.strip([chars])
old_str=' hello world itheima and ithemaAPP '
new_str=old_str.strip()
print(new_str)
hello world itheima and ithemaAPP
17) isdigit
功能:检测字符串中是否均为数字,如果是返回True,否则返回False
str_3='aabb321hh'
print(str_3.isdigit())
str_4='123654789'
print(str_4.isdigit())
False
True
18) isalpha()
功能:检测字符串中是否均为字符,如果是返回True,否则返回False
str_3='aabb'
print(str_3.isalpha())
str_4='123654abc789'
print(str_4.isalpha())
True
False
19) isalnum()
功能:检测字符串中是否均为字符或数字,如果是返回True,否则返回False
str_3='aabb'
print(str_3.isalpha())
str_4='123654abc789'
print(str_4.isalpha())
True
False
练习1:编写程序,实现通过终端输入一行字符串,统计出字符串中包含的数字的个数
str_1 = input("请输入:")
sum_1 = 0
for i in str_1:
if i >= '0' and i <= '9':
sum_1 += 1
print(sum_1)
请输入:ew46e546
5
str_1 = input("请输入:")
sum_1 = 0
for i in str_1:
if i.isdigit():
print(i)
sum_1 += 1
print("数字个数为:",sum_1)
请输入:dsdkgasr65915
6
5
9
1
5
数字个数为: 5