python学习之【第三篇】:Python中的字符串及其所具有的方法

1.前言

字符串str是Python中最常用的数据类型。我们可以使用单引号‘’或双引号“”包裹一段字符来创建字符串。

2.字符串创建

str1 = 'hello world'
str2 = 'abcdefg'

3.访问字符串中的值

字符串实际上就是字符的数组,所以我们可以使用下标索引的方式来访问字符串中的每个字符。
如果有字符串:name = 'abcdef',在内存中的实际存储如下:

那么可以通过下标的方法来取出字符串中的每个字符:

如果从头部开始取,索引为正
如果从尾部开始取,索引为负

name = 'abcdef'
print(name[0])      # 输出a     
print(name[1])      # 输出b
print(name[2])      # 输出c
print(name[-1])     # 输出f
print(name[-2])     # 输出e
print(name[-3])     # 输出d

字符串切片

切片是指对操作的对象截取其中一部分的操作。字符串、列表、元组都支持切片操作。

语法:[起始:结束:步长]

注意:选取的区间属于左闭右开型,即从"起始"位开始,到"结束"位的前一位结束(不包含结束位本身)。包左不包右

name = 'abcdef'
print(name[0:5])      # 取下标为0—4的字符,输出abcde     
print(name[2:])       # 取下标从2开始一直到结束的字符,输出cdef
print(name[1:-1])     # 取下标从1开始到倒数第2个下标之间的字符,输出bcde

4.字符串的常用方法

1.capitalize()

将字符串的第一个字母变成大写,其他字母变小写。

str = 'hello world'
print(str.capitalize())
#输出
'Hello world'

2.swapcase()

将字符串中大写转换为小写,小写转换为大写

str = 'hello world'
print(str.swapcase())
#输出
'HELLO WORLD'

3.title()

将字符串中每个单词的首字母变成大写

str = 'hello world'
print(str.title())
#输出
'Hello World'

4.upper()

将字符串所有字母都变成大写

str = 'hello world'
print(str.upper())
#输出
'HELLO WORLD'

5.lower()

将字符串所有字母都变成小写

str = 'Hello World'
print(str.lower())
#输出
'hello world'

6.rjust(width,[, fillchar])、ljust(width[, fillchar])和center(width, fillchar)

  • rjust(width,[, fillchar]):返回一个原字符串右对齐,并使用fillchar(默认空格)填充至长度 width 的新字符串
  • ljust(width[, fillchar]):返回一个原字符串左对齐,并使用 fillchar 填充至长度 width 的新字符串,fillchar 默认为空格。
  • center(width, fillchar):返回一个指定的宽度 width 居中的字符串,fillchar 为填充的字符,默认为空格。
str = 'hello'
print(str.rjust(10))
print(str.rjust(10,'*'))
print(str.ljust(10))
print(str.ljust(10,'*'))
print(str.center(10))
print(str.center(10,'*'))
#输出
'     hello'
'*****hello'
'hello     '
'hello*****'
'  hello   '
'**hello***'

7.lstrip()、rstrip()和strip()

  • lstrip():去除字符串左边的空格或指定字符。
  • rstrip():去除字符串右边的空格或指定字符。
  • strip():去除字符串左右两边的空格或指定字符。
str1 = '     hello     '
str2 = '*****hello*****'
print(str1.lstrip())
print(str2.lstrip('*'))
print(str1.rstrip())
print(str2.rstrip('*'))
print(str1.strip())
print(str2.strip('*'))
#输出
'hello     '
'hello*****'
'     hello'
'*****hello'
'hello'
'hello'

8.startswith(obj)和endswith(obj)

检查字符串是否是以 obj 开头,是则返回 True,否则返回 False。

str = 'Hello World'
print(str.startswith('Hel'))
print(str.startswith('haha'))
print(str.endswith('rld'))
print(str.endswith('hahaha'))
#输出
True
False
True
False

9.replace(oldstr, newstr)

把字符串中的 old(旧字符串) 替换成 new(新字符串)

str = 'Hello World'
print(str.replace('World', 'China'))
#输出
'Hello China'

10.split()

将字符串按照括号内的字符分割并组成列表

str = 'China-is-good'
print(str.split('-'))
#输出
['China', 'is', 'good']

11.count()

返回子字符串在字符串里面出现的次数

str = 'Hello World'
print(str.count('o'))
#输出
2

12.format()

格式化字符串

#方式一:按位置传参
str1 = '我叫{},今年{}岁'
str2 = '我叫{},今年{}岁'.format('难凉热血',18)
print(str2)
#输出
'我叫难凉热血,今年18岁'
#方式二:按索引传参
str1 = '我叫{0},今年{1}岁,大家都叫我{0}'
str2 = '我叫{0},今年{1}岁,大家都叫我{0}'.format('难凉热血',18)
print(str2)
#输出
'我叫难凉热血,今年18岁,大家都叫我难凉热血'

#方式三:按key传参
str1 = '我叫{name},今年{age}岁'
str2 = '我叫{name},今年{age}岁'.format(age=53,name='难凉热血')
print(str2)
#输出
'我叫难凉热血,今年18岁'

13.find()

在字符串中查找某个子字符串,可以整体找,也可以切片,如果找到,返回匹配到的第一个字母的索引,找不到则返回-1

str = 'Hello World,Hello China'
print(str.find('World'))
print(str.find('Hello'))
print(str.find('xxx'))
print(str.find('World',1,5))   #在第一个索引和第5个索引中寻找
#输出
6
0
-1
-1

14.index()

在字符串中查找某个子字符串,可以整体找,也可以切片,如果找到,返回匹配到的第一个字母的索引,找不到直接报错

str = 'Hello World,Hello China'
print(str.index('World'))
print(str.index('xxx'))
print(str.index('World',1,5))   #在第一个索引和第5个索引中寻找
#输出
6
报错
报错

猜你喜欢

转载自www.cnblogs.com/wangjiachen666/p/9610258.html