s = 'abc'
s = "abc"
s = 三引号赋值
s1 = 'abc'
print(id(s), id(s1))
print(len(s2)) # 获取字符串长度 len(msg)
msg = 'this is a goods boy'
print(len(msg))
s = 'abcdef'
print(s[0]) # a
print(s[1]) # b
print(s[2]) # c
print(s[3]) # d
s2 = '今天下雪啦!'
print(s2[3]) # 雪
print(s2[5]) # !
print(s2[6]) # IndexError: string index out of range
# 下标值的最大是:len-1
标号方向:
s3 = 'goodbye'
'''
b y e
-3 -2 -1
'''
print(s3[0:4]) # good
# s3[start:end] 包前不包后 与range(start,end)一样
print(s3[4:7]) # bye
print(s3[-3:-1]) # by
print(s3[4])
print(s3[-3])
# b
print(s3[4:]) # 从当前位置取到末尾
print(s3[-3:]) # 从当前位置取到末尾
# bye
print(s3[:4]) # 从头取到当前指定位置(不包含)
print(s3[:-3])
# good
'''
s[start:end:方向和步长]
如果步长是负号,从右向左开始打印
如果步长是正号,从左向右开始打印
如果不写方向和步长
s[start:end] ----> 从左向右开始打印,每次的步长为1
'''
s = 'abcd'
print(s[:]) # abcd
print(s[::]) # abcd
print(s[::1]) # abcd 表示从左向右取字符串元素打印
print(s[::-1]) # dcba 表示从右向左取字符串元素打印
s2 = '12345678'
print(s2[::2]) # 1357
# 默认的步长是1,也可以指定步长
print(s2[1::3]) # 258
print(s2[::-2]) # 8642
# 从右侧开始打印,每次取步长为2
print(s2[-2::-2]) # 7531
# 从右侧的-2开始打印,每次取步长为2
len(字符串变量)
len(s) len(‘abc’)
得到字符串的长度
s = 'abcdefg'
print(len('abcdef')) # 6
print(len(s)) # 7
在找不到元素的时候find,rfind 返回-1
而index,rindex报错
s = 'snow today abc snow today'
# 字符串内置函数:find(字符串) 得到的找到第一次出现字符串的位置
result = s.find('w')
print(result) # 3
result = s.find('abc') # 返回的是:abc 出现的位置
print(result) # 11
print(s[result:result + 3]) # abc
# 获取today
result = s.find('today')
print(s[result:result + 5]) # today
result = s.rfind('today') # right find 右侧查找
print(result) # 20
print(s[:result]) # snow today abc snow
image_path = 'https://p1.meituan.net/movie/967b253953bc7e660cfadbf9d78f67b62852693.jpg'
index = image_path.rfind('/')
print(index) # 28
print(image_path[index + 1:]) # 967b253953bc7e660cfadbf9d78f67b62852693.jpg
path = 'https://p1.meituan.net/movie/967b25.html'
index = path.rfind('.')
print(index) # 35
subfix = path[index + 1:]
print(subfix) # html
if subfix == 'jpg' or subfix == 'png' or subfix == 'gif' or subfix == 'bmp':
print('图片格式')
else:
print('非图片格式')
index = path.rfind('#') # 找的字符串在path中没有出现,则返回-1
print(index)
index = path.rindex('#') # 类似rfind,只不过没有找到字符串的时候会报错:ValueError: substring not found
print(index)
返回False/True
s = 'Hello'
result = s.startswith('h') # False
print(result)
s = 'a1.jpg'
result = s.endswith('png') # False
print(result)
s = 'hello'
result = s.isalpha() # True
print(result)
result = ' '.isspace() # True
print(result)
print('abcA'.islower()) #False
# 判断是否全部是小写字母
print('ABC'.isupper()) # True
# 判断是否全部是大写字母
统计个数
msg = '今天组织大家去电影院看:叶问终结篇和终结者'
# 统计这句话中出现了几个:终结
num = msg.count('终结')
print(num) # 2
count = 0
while msg.find('终结') != -1:
count += 1
index = msg.find('终结')
msg = msg[index + 2:]
print(count) # 2
s = 'abcaaabbcdjfkbcxyzbcabc'
num = s.count('bc')
print(num) # 5
replace(old,new,count):替换 用新的字符串替换旧字符串,默认全部替换,也可以通过count指定替换次数
s = 'abcaaabbcdjfkbcxyzbcabc'
result = s.replace('bc', 'BC', 3)
print(result) # aBCaaabBCdjfkBCxyzbcabc
path = "https://m.maoyan.com/bargain/list?lch=rdt&scene=list"
result = path.split('?')
print(result) # ['https://m.maoyan.com/bargain/list', 'lch=rdt&scene=list']
result = path.split('/')
print(result) # ['https:', '', 'm.maoyan.com', 'bargain', 'list?lch=rdt&scene=list']
s = '香蕉#苹果#橘子#柚子#梨'
result = s.split('#', 2)
print(result) # ['香蕉', '苹果', '橘子#柚子#梨']
result = s.rsplit("#")
print(result) # ['香蕉', '苹果', '橘子', '柚子', '梨']
s = '香蕉\n苹果\n橘子\n柚子\n梨'
result = s.splitlines() # 安装换行符进行分割
print(result) # ['香蕉', '苹果', '橘子', '柚子', '梨']
s = '香蕉A苹果A橘子'
result = s.rpartition('A')
print(result) # ('香蕉A苹果', 'A', '橘子')
s = 'hello world'
result = s.capitalize()
print(result) # Hello world
s1 = 'this is a book'
result = s1.capitalize()
print(result) # This is a book
result = s1.title()
print(result) # This Is A Book
print('abc'.upper()) # ABC
print('abc'.isupper()) # False
print('ABC'.lower()) # abc
print('ABC'.islower()) # False
result = '-'.join('abcd')
print(result) # a-b-c-d
result = '-'.join(['香蕉', '橘子', '西瓜']) # 返回的结果是:str
print(result) # 香蕉-橘子-西瓜
# ['牛奶','薯片','辣条','可乐'] ---> str
l = ['牛奶', '薯片', '辣条', '可乐']
result = ','.join(l)
print(result, type(result)) # 牛奶,薯片,辣条,可乐
ljust,rjust,center —> 使用默认空格调整打印格式
lstrip,rstrip,strip —> 去除字符串中空格
s = 'helloworld'
result = s.ljust(20,' ') # left
print(result)
result = s.rjust(20,'#') # right
print(result)
result = s.center(20,'#')
print(result)
print('欢迎来到英雄联盟'.center(30))
helloworld##########
helloworld
#####helloworld#####
欢迎来到英雄联盟
result = ' hello '.strip()
print(result)
result = ' hello '.lstrip()
print(result)
result = ' hello '.rstrip()
print(result)
hello
hello
hello