python入门到精通教程04-字符串

python字符串创建

常用单引号('')或者双引号("")创建字符串多行文本用三个引号(''' '''或者""" """)Python 不支持单字符类型,单字符在 Python 中也是作为一个字符串使用。

单双引号与三个引号区别:

单双引号不支持字符串本身换行三个引号可以保留字符串原格式,包括换行

什么时候用单引号什么时候用双引号:

一般字符串有单引号时用双引号,同理字符串有双引号时使用单引号

字符串创建实例

<code>str1 = 'hello,world'
str2 = "hello python"
str3 = "I'm a student" #字符串里有单引号,使用双引号创建字符串
str4 = """人生苦短
我要学python
"""/<code>

字符串连接

使用+连接字符串注意:不能将字符串与非字符串用+连接,如'hello' + 5,会报错str(5):将数字5转为字符串5

代码示例解析

<code>str = 'hello '+'world'
print(str) #输出 hello world
str2 = 'hello' + 5 #【出错:字符串+非字符串(数字)】
str3 = 'hello' +str(5) #正确
print(str3) #输出hello5/<code>

字符串运算

连接:+

原始字符:r或者R

是否存在某个字符:in 或者 not in

代码示例解析:

<code>str = 'hello '+'world' #字符串连接+
print(str) #输出 hello world
str2 = r"hello\\n" #原始字符r,不会将\\n转为换行符
print(str2) #输出hello\\n
str3 = "hello\\n"
print(str3) #输出hello并换行,将\\n转为换行符
res = 'h' in 'hello' # 字符串hello是否存在字符h
print(res) # True
res2 = 'h' not in 'hello' # 字符串hello是否不存在字符h
print(res2) #False/<code>

常用转义符

\\\\:单个反斜杠\\':单引号\":双引号\\n:换行\\t:横线制表符,相当于Tab键

代码示例解析:

<code>str = 'male \\\\ female' #\\\\单个反斜杠转义符
print(str) #输出:male \\ female
str2 = 'I\\'m a student' #单引号转义符
print(str2) #输出:I'm a student
str3 = "I am a student.\\nI am 18 years old." #换行转义符
print(str3)
"""
输出
I am a student.
I am 18 years old.


"""
str4 = 'I am a student.\\tI am 18 years old.' #Tab键换行符
print(str4) #输出I am a student.\tI am 18 years old./<code>

字符串切片

str[index]:index为下标索引,从0开始,如果索引值为负数则是倒着开始str[start:end]:读取从索引start开始到索引end-1结束。【注意:不含end,是end-1】str[start:end:step]:读取从索引start开始,每隔step直到索引end-1结束str[:]:读取整个字符串str[:end]:读取从索引0开始到索引end-1结束str[start:]:读取从索引start开始到结尾

代码示例解析

<code>str = 'abcdefg'
print(str[1]) #索引为1,输出:b
print(str[1:6]) #从索引1到5,输出:bcdef
print(str[1:6:2]) #从索引1开始间隔2,到索引5结束,输出:bdf
print(str[:]) #全部,输出:abcdefg
print(str[1:])#从索引1开始到结尾,输出:bcdefg
print(str[:5]) #从索引0开始到索引4,即前5个,输出abcde/<code>

字符串常用函数

string.capitalize():将字符串第一个字母转为大写string.title():将字符串中的单词首字母转为大写string.replace(old,new[,count]):替换string.strip():去除左右空格string.split(sep):拆分字符串string.count(str, beg= 0,end=len(string)):统计字符串出现的次数string.endswith(suffix, beg=0, end=len(string)):判断字符串是否以suffix结尾string.startswith(substr, beg=0,end=len(string)):检查字符串是否是以指定子字符串 substr 开头,是则返回 True,否则返回 False。如果beg 和 end 指定值,则在指定范围内检查。string.find(sub[,start[,end]]):查找字符:检测 sub 否包含在字符串中,如果指定范围 beg 和 end ,则检查是否包含在指定范围内,如果包含返回开始的索引值,否则返回-1string.isalnum():如果字符串至少有一个字符并且所有字符都是字母或数字则返 回 True,否则返回 Falsestring.isalpha():如果字符串至少有一个字符并且所有字符都是字母则返回 True, 否则返回 Falsestring.isdigit():如果字符串只包含数字则返回 True 否则返回 False..string.isnumeric():如果字符串中只包含数字字符,则返回 True,否则返回 Falsestring.isdecimal():检查字符串是否只包含十进制字符,如果是返回 true,否则返回 false。string.upper():将字符串全部转为大写string.lower():将字符串全部转为小写len(string):返回字符串长度

代码示例解析:

<code>str = 'hello,python. i like python'
print(str.capitalize()) #将字符串第一个字母转为大写,输出Hello,python. i like python
print(str.title()) #将单词首字母变成大写,输出Hello,Python. I Like Python
print(str.replace('python','world')) #将str中的python替换world,输出hello,world. i like world
print(str.replace('python','world',1)) #将str中的python替换world且只替换一次,输出hello,world. i like python
print(' i am a student '.strip()) #去掉左右边空格,输出:i am a student
str2 = 'banner,apple,orange'print(str2.split(',')) #,分隔符,生成列表,输出['banner', 'apple', 'orange']
print(str2.count(','))#统计字符串str2中,的个数,输出2
print(str2.endswith('orange')) #True
print(str2.startswith('ban')) #True
print(str2.find('app')) #输出7
print(str2.find('app2')) #输出-1
print(len(str2)) #输出19/<code>

后续课程将会持续更新,欢迎大家点赞评论交流~~~