21 篇文章 3 订阅

# 1、切片

name = "abcdefg"

print(name[2:5:1]) # cde
print(name[2:5]) # cde
print(name[:5]) # abcde
print(name[1:]) # bcdefg
print(name[:]) # abcdefg
print(name[::2]) # aceg
print(name[:-1]) # abcdef, 负1表示倒数第⼀一个数据
print(name[-4:-1]) # def
print(name[::-1]) # gfedcba


name = "abcdefg"
print(name[::-1]) # gfedcba


# 2、查找

## 2.1 find()方法：

find()：检测某个子串是否包含在这个字符串中，如果在返回这个子串开始的位置下标，否则返回-1。

mystr = "hello world and itcast and itheima and Python"

print(mystr.find('and'))  # 12
print(mystr.find('and', 15, 30)) # 23
print(mystr.find('ands')) # -1


## 2.2 index()方法：

index()：检测某个子串是否包含在这个字符串中，如果在返回这个子串开始的位置下标，否则报异常。

mystr = "hello world and itcast and itheima and Python"

print(mystr.index('and')) # 12
print(mystr.index('and', 15, 30)) # 23
print(mystr.index('ands')) # 报错


## 2.3 count()方法：

count()：返回某个子串在字符串中出现的次数。

mystr = "hello world and itcast and itheima and Python"
print(mystr.count('and')) # 3
print(mystr.count('ands')) # 0
print(mystr.count('and', 0, 20)) # 1


# 3、修改

## 3.1 replace()：替换

mystr = "hello world and itcast and itheima and Python"

# 结果：hello world he itcast he itheima he Python
print(mystr.replace('and', 'he'))  # 默认都替换了

# 结果：hello world he itcast he itheima he Python
print(mystr.replace('and', 'he', 10))

# 结果：hello world and itcast and itheima and Python
print(mystr)  # 说明字符串是不可变类型，返回的值才是替换后的字符串


## 3.2 split()：按照指定字符分割字符串，返回一个列表

mystr = "hello world and itcast and itheima and Python"

# 结果：['hello world ', ' itcast ', ' itheima ', ' Python']
print(mystr.split('and'))

# 结果：['hello world ', ' itcast ', ' itheima and Python']
print(mystr.split('and', 2))

# 结果：['hello', 'world', 'and', 'itcast', 'and', 'itheima', 'and', 'Python']
print(mystr.split(' '))

# 结果：['hello', 'world', 'and itcast and itheima and Python']
print(mystr.split(' ', 2))

# 结果：['hello', 'world', 'and', 'itcast', 'and', 'itheima', 'and', 'Python']
print(mystr.split())  # 默认是按空格分割


## 3.3 join()：

⽤一个字符或⼦串合并字符串，即将多个字符串合并为一个新的字符串。

list1 = ['chuan', 'zhi', 'bo', 'ke']
t1 = ('aa', 'b', 'cc', 'ddd')

# 结果：chuan_zhi_bo_ke
print('_'.join(list1))

# 结果：aa...b...cc...ddd
print('...'.join(t1))


## 3.4 capitalize()：将字符串第一个字符转换成大写

mystr = "hello world and itcast and itheima and Python"

# 结果：Hello world and itcast and itheima and python
print(mystr.capitalize())


## 3.5 title()：将字符串每个单词首字母转换成大写

mystr = "hello world and itcast and itheima and Python"

# 结果：Hello World And Itcast And Itheima And Python
print(mystr.title())


## 3.6 lower()：将字符串中大写转小写

mystr = "hello world and itcast and itheima and Python"

# 结果：hello world and itcast and itheima and python
print(mystr.lower())


## 3.7 upper():将字符串中小写转大写

mystr = "hello world and itcast and itheima and Python"

# 结果：HELLO WORLD AND ITCAST AND ITHEIMA AND PYTHON
print(mystr.upper())


# 5、判断

## 5.1 startswith()：

mystr = "hello world and itcast and itheima and Python "

# 结果：True
print(mystr.startswith('hello'))

# 结果False
print(mystr.startswith('hello', 5, 20))


## 5.2 endswith()：

mystr = "hello world and itcast and itheima and Python"

# 结果：True
print(mystr.endswith('Python'))

# 结果：False
print(mystr.endswith('python'))

# 结果：False
print(mystr.endswith('Python', 2, 20))


## 5.3 isalpha()：

mystr1 = 'hello'
mystr2 = 'hello12345'

# 结果：True
print(mystr1.isalpha())

# 结果：False
print(mystr2.isalpha())


## 5.4 isdigit()：

mystr1 = 'aaa12345'
mystr2 = '12345'

# 结果： False
print(mystr1.isdigit())

# 结果：False
print(mystr2.isdigit())


## 5.5 isalnum()：

mystr1 = 'aaa12345'
mystr2 = '12345-'

# 结果：True
print(mystr1.isalnum())

# 结果：False
print(mystr2.isalnum())


## 5.6 isspace()：

mystr1 = '1 2 3 4 5'
mystr2 = ' '

# 结果：False
print(mystr1.isspace())

# 结果：True
print(mystr2.isspace())

12-16 720
12-18 243
03-31 6789
02-19 2252
07-19 118
02-05 240
09-17 25万+
04-10 2万+
03-26 545
04-25 219

### “相关推荐”对你有帮助么？

• 非常没帮助
• 没帮助
• 一般
• 有帮助
• 非常有帮助

Ma Sizhou

¥2 ¥4 ¥6 ¥10 ¥20

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、C币套餐、付费专栏及课程。