字符串是字符组成的序列。有几个处理字符串的算法,包括查找,排序,比较,变换。Python 字符串不可更改的,也就是字符串被创建后不能被改变。将一个字符序列用单引号,或者双引号,三引号括起来,并赋值给一个变量,就定义了一个字符串。
str = ‘Hellow World!’
str = “Hellow World!”
str = “””Sunday Monday Tuesday”””
访问字符串内的字符
使用中括号 []和下标索引访问字符串内的字符,索引从0开始。
str = ‘Hellow World!’
print(str [0]) # 输出 “H”
print(str [7]) # 输出 “W”
print(str [0:6]) #输出 Hellow
print(str [7:12]) #输出 World
Python 可以使用负数索引。
索引 -1 指向最后一个元素,-2指向倒数第二个元素,等等。
print(str [0:-6]) # 输出 Hellow
print(str [7:-1]) # 输出 World
字符串连接
将两个或者多个字符串合并成一个,称为连接,使用 “+”操作符来完成连接操作。
str1 = ‘Hellow ‘
str2 = ‘ World!’
print(str1 + str2)
输出
Hellow World!
反转字符串
Python字符串可以取块,从一个位置到另一个位置,可向前,也可向后,由给定的递增量(步长)决定。取块在字符串下标里面表示:string[下标]
下标用冒号隔开:string[开始:结束:步长]
如果步长是-1,则反转字符串。
str = ‘Python String’
print(str[::-1])
输出
gnirtS nohtyP
字符串的方法
Python有一些内建的方法可以处理字符串。用这些方法可以很容易的编辑和操作字符串。内建方法定义在Python语言内,下面是一些最常用的字符串方法。
Python 字符串的len() 方法
字符串len()方法返回字符串的长度。
str = “Hellow World!”
print(len(str))
输出
13
Python 字符串的count() 方法
字符串count() 方法返回子字符串在原字符串中出现的次数。
str = “Python is Object Oriented”
substr = “Object”
print(str.count(substr)) # 返回1, 因为单词 Object在str里出现了一次
输出
1
Python 字符串的index() 方法
字符串index() 方法返回子字符串在给定字符串里面的索引位置。index(子串,start,end)
end(可选),默认是字符串的长度。
str = “Python is Object Oriented”
substr = “is”
print(str.index(substr))
输出
7
Python 字符串的upper() 方法
字符串upper() 将字符串字符都转换成大写,并返回新的字符串。
str = “Python is Object Oriented”
print(str.upper())
输出
PYTHON IS OBJECT ORIENTED
Python 字符串的lower() 方法
字符串lower() 将字符串字符都转换成小写,并返回新的字符串。
str = “Python is Object Oriented”
print(str.lower())
输出
python is object oriented
Python 字符串的startswith() 方法
字符串startswith() 方法返回 True 或 False,根据字符串是否以子字符串开始来判断。
str = “Python is Object Oriented”
print(str.startswith(“Python”))
print(str.startswith(“Object”))
输出
True
False
Python 字符串的endswith() 方法
字符串endswith() 方法返回 True 或 False,根据字符串是否以子字符串结束来判断。
str = “Python is Object Oriented”
print(str.endswith(“Oriented”))
print(str.endswith(“Object”))
输出
True
False
Python 字符串的split() 方法
字符串split() 方法将字符串打断成较小的字符串,依据分隔符或者指定字符。
str = ‘Python is Object Oriented’
print(str.split())
输出
[‘Python’, ‘is’, ‘Object’, ‘Oriented’]
例子
str = ‘Python,is,Object,Oriented’
print(str.split(‘,’))
输出
[‘Python’, ‘is’, ‘Object’, ‘Oriented’]
例子
str = ‘Python,is,Object,Oriented’
print(str.split(‘,’,2))
输出
[‘Python’, ‘is’, ‘Object,Oriented’]
Python将打断的字符串作为一个列表返回。
str = ‘Python,is,Object,Oriented’
sList = str.split(‘,’)
for temp in sList:
print (temp)
输出
Python is Object Oriented
Python字符串的 join() 方法
递归性的连接一个字符串的全部元素。
str = “Python is Object Oriented”
tmp = “-”
print (tmp.join(str))
输出
P-y-t-h-o-n- -i-s- -O-b-j-e-c-t- -O-r-i-e-n-t-e-d
Python 字符串的find()方法
找出指定字符串在原字符串中首次出现的索引位置。如果不存在,则返回 -1。
str = “Python is Object Oriented”
st = “Object”
print (str.find(st))
print (str.find(st,20)) #从第20的位置查找
输出
10
-1
Python 字符串的strip()方法
去掉字符串的左右两边的指定字符,默认是空格,并且返回新的字符串。
str = ” Pytho 是面向对象的 ”
print (str.strip())
输出
Pyth 是面向对象的
Python 字符串的rstrip() 方法
返回去掉右边空格的字符串的拷贝。
str = ” Pytho 是面向对象的 ”
print (str.rstrip())
输出
Pyth 是面向对象的
Python 字符串的lstrip() 方法
返回去掉左边空格的字符串的拷贝。
str = ” Python 是面向对象的 ”
print (str.lstrip())
输出
Python 是面向对象的
Python 基础