1.输入
字符串输入有四种方式:
1)单引号输入
2)双引号输入
3)三单引号输入
4)三双引号输入
单引号和双引号的输入效果是一样的,三单引号输入和三双引号输入在输入时换行,输出时也会换行
# 单引号字符串 换行会自动增加 \
a = 'my name ' \
'is tom'
print(a)
print(type(a))
# 双引号字符串 换行会自动增加 \
b = "my name " \
"is tom"
print(b)
# 三引号字符串 支持换行,换行后输出也会换行
c = '''my name is tom,
from guandong'''
print(c)
# 三引号字符串 支持换行,换行后输出也会换行
d = """my name is tom,
from guandong"""
print(d)
2.字符串切片
语法:字符串序列[开始位置坐标,结束位置坐标,长度]
注意:1)输出的数据不包含结束位置的数据
2)如果坐标不标名,默认开始坐标为 0,默认结束坐标为最后一个
3)坐标和长度正负数都可以,长度是选取间隔,如果是负数,表示倒叙
str1 = 'abcdefg'
print(str1[0:3:1]) # 输出 abc
print(str1[0:-1:2]) #输出 ace
print(str1[-4:-1:1]) #输出 def
print(str1[1:4:-1]) # 输出 无
print(str1[6:2:-1]) # 输出 gfed
3.字符串查找
- find() 函用法
字符串序列.find(子串,开始位置下标,结束位置下标) |
注意:当查找的子串不存在时返回 -1
str1 = 'hello world and itheima and python'
# 用法 find(字串,开始位置下标,结束位置下标)
# find() 查找
print(str1.find('and'))
print(str1.find('and', 15, 30))
print(str1.find('ands')) # 不存在返回 -1
- index() 函数用法
字符串序列.index(子串,开始位置下标,结束位置下标) |
注意:当查找的子串不存在时会报错
str1 = 'hello world and itheima and python'
# 用法 index(字串,开始位置下标,结束位置下标)
# index() 查找
print(str1.index('and'))
print(str1.index('and', 15, 30))
print(str1.index('ands')) # 不存在报错
- count() 函数用法
字符串序列.count(子串,开始位置,结束位置) 会返回子串的个数 |
str1 = 'hello world and itheima and python'
print(str1.count('and')) # 返回 2
print(str1.count('and',0,20)) # 返回 1
- rfind()和rindex() 两个函数的用法
字符串序列.rfind(子串,开始位置,结束位置) |
字符串序列.rindex(子串,开始位置,结束位置) |
都是从右边开始查找
str1 = 'hello world and itheima and python'
# rfing() 和find用法一样 但是从右侧开始查找
print(str1.rfind('and'))
# rindex() 和index用法一样 但是从右侧开始查找
print(str1.rindex('and'))
4.字符串修改
- replace() 函数用法:替换
字符串序列.replace(子串,要替换的字符串,参数) |
该函数不会改变原来的字符串,会返回一个新的字符串,参数表示要替换的次数
str1 = 'hello world and itheima and python and c++'
# replace() 字符串替换
new_str = str1.replace('and', 'he') # 该函数不会改变原来的字符串,会返回一个新的字符串
new_str = str1.replace('and', 'he', 1) #参数表示替换的次数
print(str1, "\n", new_str)
- split() 函数的用法:分割字符串
字符串序列.split(字符串,参数) |
该函数会返回一个列表,但是会丢失分割的字符串,参数表示要分割的次数
# split() 分割函数,会返回一个列表,丢失分割字符
list1 = str1.split('and')
list2 = str1.split('and', 2) # 参数表示分割次数
print(list1)
print(list2)
输出
['hello world ', ' itheima ', ' python ', ' c++']
['hello world ', ' itheima ', ' python and c++']
- join() 函数用法:合并字符串
字符串或字符.join(列表) |
# join() 合并字符串
# 字符串或字符.join(列表)
mylist = ['a', 'b', 'c']
new_str = ' '.join(mylist)
print(new_str)
# 输出
a b c
- capitalize() 函数用法:把字符串首字母转换成大写
字符串序列.capitalize() |
- title() 函数用法:把所有单词首字母转换成大写
字符串序列.title() |
- upper() :把字符串所有字母转换成大写
- lower() :小写转大写
- lstrip():删除左侧空白字符
- rstrip:删除右侧空白字符
- strip:删除左右空白字符
- ljust:左对齐
- rjust:右对齐
- center:中间对齐
# upper() 把所有字母转换成大写 lower() 小写转大写
# new_str1 = str1.upper()
# print(new_str1)
# str2 = ' hello world and itheima and python and c++ '
# # lstrip() 删除左侧空白字符
# new_str1 = str2.lstrip()
# print(new_str1)
# # rstrip() 删除右侧空白字符
# new_str2 = str2.rstrip()
# print(new_str2)
# # strip() 删除左右空白字符
# new_str3 = str2.strip()
# print(new_str3)
str3 = 'hello world and itheima'
# ljust() 左对齐
new_str1 = str3.ljust(50, '.')
print(new_str1)
# rjust() 右对齐
new_str2 = str3.rjust(50, '.')
print(new_str2)
# center() 中间对齐
new_str3 = str3.center(50, '.')
print(new_str3)
5.字符串判断
- startswith() 函数的用法:判断是否以某个字符串开始,如果是返回 True,否则返回 False
字符串序列.startswith(子串,开始位置下标,结束位置下标) |
str1 = 'hello word and itheima'
# startswith() 判断是否以某个字符串开始 如果是返回 True 否则返回 False
# 语法 字符串序列.startswith(子串,开始位置下标,结束位置下标)
print(str1.startswith('hell'))
print(str1.startswith('lo', 3, 10))
print(str1.startswith('hels'))
- endswith() 函数的用法:判断是否以某个字符串结尾,如果是返回 True,否则返回 False
字符串序列.endswith(子串) |
str1 = 'hello word and itheima'
# # endswith() 判断是否以某个字符串结尾
print(str1.endswith('ima'))
print(str1.endswith('im'))
- isalpha():如果字符串全是字母,返回True,否则返回False
- isdigit():如果字符串全是数字则返回True,否则返回False
- isalnum():数字或字母或组合都返回True
- isspace():如果是空白返回True