python——字符串&切片

字符串对象:

1、字符串定义:
(1) 定义:字符串就是一系列字符,在python中,用引号(单引号,双引号,三引号都可以)括起来的都是字符串。例如:“kaikai” , “hello world” ,“xixi is a girl”
(2)输出字符串
直接在print函数中输出字符串。如:print(“kaikai”)
(3)拼接字符串python中使用加号(+)来拼接字符串。如:
在这里插入图片描述
2.字符串的常见方法:
(1)字符串大小写转换
lower() upper() title() capitalize()
== 这4个函数都是改变返回值,不改变原值。==

在控制台演示如下:

 >>> s="the rain stopped and the sky cleared up"
>>> s.upper() #把字符串中小写字符变成大写字符
'THE RAIN STOPPED AND THE SKY CLEARED UP'
>>> s
'the rain stopped and the sky cleared up'
>>> s.lower()  # 把字符串中大写字符变成小写字符
'the rain stopped and the sky cleared up'
>>> s
'the rain stopped and the sky cleared up'
>>> s.title() #将字符串转换为标题格式
'The Rain Stopped And The Sky Cleared Up'
>>> s
'the rain stopped and the sky cleared up'
>>> s.capitalize()
# 让字符串首字母大写(规范化每段的第一句话)
'The rain stopped and the sky cleared up'
>>>

(2)字符串格式(居中or左右对齐)

center(width,[fillchar]) # 设置字符串安装长度居中,fillchar默认是空格,可以自定义
ljust # 左对齐,fillchar默认是空格,可以自定义
rjust # 右对齐,fillchar默认是空格,可以自定义
count() # 统计字符或者字符串出现的次数
endswith() # 判断字符串是否以xxx结尾
startswith() # 判断字符串是否以xxx开头
example:

>>> s.center(50)
'     the rain stopped and the sky cleared up      '
>>> s.center(50,"+")
'+++++the rain stopped and the sky cleared up++++++'
>>> s.ljust(50)
'the rain stopped and the sky cleared up           '
>>> s.rjust(50)
'           the rain stopped and the sky cleared up'
>>> s.count("a")
3
>>> s.endswith("u")
False
>>> s.endswith("p")
True

(3)查找字符
index # 查找字符或者字符串在字符串中第一次出现的位置;如果字符或者字符串不存在,则抛出异常
rindex # 查找字符或者字符串在字符串中最后一次出现的位置
find # 查找字符或者字符串在字符串中第一次出现的位置,如果字符或者字符串不存在,则返回-1
rfind # 查找字符或者字符串在字符串中最后一次出现的位置

>>> s
'the rain stopped and the sky cleared up'
>>> s.index("a")
5
>>> s.rindex("a")
32
>>> s.find("a")
5
>>> s.find("w")
-1
#find()如果字符或者字符串不存在,则返回-1
>>> s.index("w") 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: substring not found
#index()如果字符或者字符串不存在,则抛出异常

(4) encode(charset)
decode(charset)

>>> s.encode("utf-8")
b'the rain stopped and the sky cleared up'
#encode python3提供python用来将字符串转换为字节的一个方法
>>> ss=s.encode("utf-8")
>>> ss
b'the rain stopped and the sky cleared up'
>>> ss.decode("utf-8")
'the rain stopped and the sky cleared up'
# 将字节转换为字符串

(5)字符判断
format # 用来格式化字符串的
islower # 判断是否都是小写字母
isupper # 判断是否都是大写字母
istitle # 判断字符串是否是标题
isdigit # 判断是不是数字
isalnum # 判断是否都由有效符号
isalpha # 判断是否都由字母组成

>>> a=1
>>> b=2
>>> print("{},{}".format(b,a))
2,1
>>> s
'the rain stopped and the sky cleared up'
>>> s.islower()
True
>>> s.isupper()
False
>>> s='the rain stopped and the sky cleared up'
>>> s.isdigit()
False
>>> s="123"
>>> s.isdigit() #判断是不是数字,是数字返回True
True
>>> s.isalnum()	# 判断是否都由有效符号
True
>>> s.isalpha()# 判断是否都由字母组成,因为有空格返回false
False
>>> s="hahaha"
>>> s.isalpha()
True

(6)strip&join&split(“符号”)
replace(“原字符串”, “新值”) # 替换对应的字符串
strip # 清除字符串两侧的空格
lstrip # 清除左侧空格
rstrip # 清除右侧空格

>>> s="today is sunday"
>>> s.split("i")
['today ', 's sunday']
>>> ss=s.split(" ")#按照特定的符号,将字符串切割,返回一个列表	
>>> ss
['today', 'is', 'sunday']
>>> " ".join(ss)
'today is sunday'# 按照特定的符号,将一个可迭代对象拼接成字符串
>>> s="today** is** sunday"
>>> ss=s.split("*")	
>>> ss
['today', '', ' is', '', ' sunday']
>>> ss=s.split("**")
>>> ss
['today', ' is', ' sunday']
>>> "**".join(ss)
'today** is** sunday'
>>> s="today is sunday"
>>> s.replace("is","is a good")  # 替换对应的字符串
'today is a good sunday'
>>> s
'today is sunday'
>>> ss=s.center(30)
>>> ss
'       today is sunday        '
>>> ss.strip() 	# 清除字符串两侧的空格
'today is sunday'
>>> ss.lstrip() 	# 清除左侧空格
'today is sunday        '
>>> ss.rstrip() 	# 清除右侧空格
'       today is sunday'

切片:

==python提供大家用来切割可迭代对象(list,tuple,dict)
集合并不能使用切片操作 ==

 iterable[start:]		# 从start位置开始切割,切到末尾
 iterable[start:end]		# 从start位置开始切割,切到end位置为止,注意end并不包含 [)区间
 iterable[start:end:step]	# 从start位置截取,到end位置结束,step为步长,步长默认是1

如果步长为正,则从左到右切
如果步长为负,则从右向左切,但是下标还是从左而右数的
同时python也提供负索引(从右向左数,注意:最后一个元素是-1,倒数第二个是-2…)

切列表:

>>> ls=[100,89,"xixi",23,45,"haha",89]
>>> ls[2:]
['xixi', 23, 45, 'haha', 89]
>>> ls[2:6]
['xixi', 23, 45, 'haha']
>>> ls[2:6:2]
['xixi', 45]

切元组:

>>> t=tuple((11,22,34,56,"xixi",'haha',100))
>>> t
(11, 22, 34, 56, 'xixi', 'haha', 100)
>>> type(t)
<class 'tuple'>
>>> t[2:9]
#切片的start位置没有超出下标,则从start位置切完
(34, 56, 'xixi', 'haha', 100)
>>> t[2:]
(34, 56, 'xixi', 'haha', 100)
>>> t[6:3:-1]
(100, 'haha', 'xixi')
>>> t[::2] #步长为正,从左到右切,start位置是0
(11, 34, 'xixi', 100)
>>> t[::-1]
#步长为负,从右向左切步长是1,但是下标还是从左而右数的
(100, 'haha', 'xixi', 56, 34, 22, 11)
>>>
list = [1,2,3,4,5,6,7,8,9,10]
print(list[0:]) #从第一位取到最后一位
print(list[1:3]) #从二个元素取到第三个元素但不包含第三个元素
print(list[::2]) #从第一个元素取到最一个元素,每隔2步取一次值
print(list[::-1]) #可以起到倒序的作用

在这里插入图片描述
面试题:
1、使用切片去切割数据,如果超越了下标
不会报错,会返回一个空列表[]
如果切片的start位置没有超出下标,则从start位置切完
如果start位置超越下标,就返回空列表

在这里插入图片描述
2、在python中怎么将列表反向输出,
请不要使用系统提供的api,请用一行代码表示

法一:使用系统提供的api
>>> ls=[100,89,"xixi",23,45,"haha",89]
>>> ls.reverse()
>>> ls
[89, 'haha', 45, 23, 'xixi', 89, 100]
法二:不使用系统提供的api,并用一行代码表示
>>> ls=[100,89,"xixi",23,45,"haha",89]
>>> ls[::-1] 
 #步长为负,从右向左切步长是1,但是下标还是从左而右数的
[89, 'haha', 45, 23, 'xixi', 89, 100]
发布了47 篇原创文章 · 获赞 11 · 访问量 2192

猜你喜欢

转载自blog.csdn.net/qq_45630589/article/details/104977594