python基础入门——容器补充(字符串)

字符串

-定义

            单引号,双引号,三引号
>>> s1 = '123'
>>> s2 = "haha"
>>> s3 = """ this is a cat"""
>>> s4 = str("1 2 3 4")
 注意:''' 注释‘’’   “”“ 注释 ”“”    
 这里也可以定义字符串
>>> s1 = """             
... haha
... guaiguai     
...
... """
>>> s1
'\nhaha\nguaiguai\n\n'
>>> type(s1)
<class 'str'>

字符串里的方法

 (1) 'capitalize'		# 将字符串的首字母大小
>>> s1 = "this is a cat"
>>> s1.capitalize()
'This is a cat'
(2)	 'center'		# 将字符串居中,第二个参数表示填充的符号,符号可自定义
>>> s1.center(30)
'        this is a cat         '
>>> s1.center(40,"*")
'*************this is a cat**************'
(3)		 'count'		# 统计字符串中出现字符或者字符串次数
>>> s1.count("a")
2
>>> s1.count("is")
2
	(4) 'encode'(重要)	# 该方法就可以将字符串转换为字节
                                               以后建议大家进行编码转换的时候统一使用utf-8
	                                     	 注意:编码和解码一定要使用同一个标准!!!
	                                        	与它对应的是字节的decode(解码)
>>> s1 ="this is a cat"
>>> s = s1.encode("utf-8")
>>> s
b'this is a cat'
>>> type(s)
<class 'bytes'>
>>> s.decode("utf-8")
'this is a cat'
>>> s.decode("gbk")
'this is a cat'

一般用什么编码就用什么解码,对于英文字符不影响,但对于汉字就必须如此。

>>> s2 = "哈哈哈哈"
>>> t = s2.encode("utf-8")
>>> t.decode("utf-8")
'哈哈哈哈'
>>> t.decode("gbk")
'鍝堝搱鍝堝搱'

注:  
使用encode方法将对象可以直接变成字节,
但进行编码和解码时必须要重新加入一个对象,才能完成解码

>>> s3 = "djdhkjkd"
>>> s3.encode("utf-8")  
b'djdhkjkd'
>>> type(s3)
<class 'str'>
>>> s3.decode("utf-8")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'str' object has no attribute 'decode'
>>>
(5)	'endswith'		# 判断字符是否以xx结尾
	    'startswith'		# 判断字符串是否以xxx开头
>>> s1.startswith("t")
True
>>> s1.startswith("h")
False

>>> s1.endswith("h")
False
>>> s1.endswith("t")
True
 (6)		'find'			# 查找字符串中某个字符或者字符串第一次出现的位置,注意:如果不存在,则返回-1
           	rfind			   # 找最后一次出现的位置
>>> s1.find("i")
2
>>> s1.rfind("i")
5
>>> s1.find("e")
-1
(7)		'index'			# 查找字符串中某个字符或者字符串第一次出现的位置,注意:如果不存在,则抛出异常
	         rindex			# 找最后一个	

>>> s1.index("i")
2
>>> s1.rindex("i")
5
>>> s1.rindex("e")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: substring not found
(8)	'format'(很重要)	# python3推出新的一种格式化字符串的方式
>>> a = 35
>>> b = 40
>>> print("%s %s" %(a,b))
35 40
>>> print(a , b)
35 40
>>> print("{} - {} = {}".format(a,b,(a - b)))
35 - 40 = -5
(9)		join			# 用来拼接字符串,注意参数是一个可迭代对象。	“ 符号”可自己定义,以特定的规则拼接字符串
字符串
>>> s1
'this is a cat'
>>> "*".join(s1)    
't*h*i*s* *i*s* *a* *c*a*t'
列表
>>> "*".join(["xixi","haha"])
'xixi*haha'
(10)	split			# 分割字符串,以特定的规则分割字符串   
	    rsplit			#  不加r 是从前向后,加是从后向前
>>> s1
'this is a cat'
>>> s1.split(" ")     #以空格分割
['this', 'is', 'a', 'cat']
>>> s1.split("i")      #以i分割
['th', 's ', 's a cat']
(11) 	lower			#字符串转小写
     	upper			# 转大写 	
>>> s1.upper()
'THIS IS A CAT'
>>> s1.lower()
'this is a cat'
(12)	title			# 转换字符串为一个符合标题的规则
在这里插入代>>> s1.title()
'This Is A Cat'码片
(13)		strip			# 清除字符串两边的空格
           	rstrip			# 清除右边的空格
	        lstrip			# 清除左边空格
>>> s2 = "   good   ff   fds   dfd     "
>>> s2
'   good   ff   fds   dfd     '
>>> s2.strip()
'good   ff   fds   dfd'
>>> s2.lstrip()
'good   ff   fds   dfd     '
>>> s2.rstrip()
'   good   ff   fds   dfd'
(14)	replace			# 替换字符串
>>> s1
'this is a cat'
>>> s1.replace("cat","dog")
'this is a dog'
二	(下面)会用即可,下面这些都是判断方法,所以返回都是True,False
        (1) istitle			# 判断字符串是不是标题,标题是 每个单词的首字母都是大写。
>>> "Hejj cdfd".istitle()
False
>>> "Hejj Acdfd".istitle()
True
(2)	isspace			# 判断是不是空白字符	
>>> s1.isspace()
False
>>> s1
'this is a cat'
>>> " ".isspace()
True
(3)	islower			# 判断是不是小写字母
(4)	isupper			# 判断是不是大字母
>>> s1.islower()
True
>>> "AHSJDHiufjjjkj".islower()
False

>>> "AHSJDH".isupper()
True
>>> "AHSJDHshd".isupper()
False
(5)	isalnum			# 判断是不是有字母和数字组成
       	isalpha			# 判断是不是有字母组成
      	isdigit			# 判断是不是数字组成
>>> "fjhdf1234".isalnum()
True

>>> "fdjfhu1213".isalpha()
False
>>> "fdjfhu".isalpha()
True

>>> "12".isdigit()
True

切片操作: 用来分割容器

     1.是python特有的。
     2.针对有序和可变类型,所以字符串(不可变类型)和列表应用的比较多。

格式:

  [角标1:角标2:步长]     
                      取值范围[角标1,角标2),不写默认步长为1

注:

   步长为正时,从左往右依次取数,
      为负时,从右往左开始取。
截取片段时:
   下标不变从零开始从左向右的顺序
   这里介绍的都是正索引。

1,字符串

>>> s = "this is a pen"1)后角标什么都不写时,默认取到最后一个元素
>>> s[3:]
's is a pen'2)前角标什么都不写时,默认从0角标开始取元素
>>> s[:6]
'this i'3>>> s[5:9]
'is a'4)自定义步长
>>> s[1:6:2]
'hsi'5)步长为负
>>> s[8:4:-1]
'a si'

2.列表

>>> a = [2,3,4,1,2,4,6]
>>> a[2:6]
[4, 1, 2, 4]

>>> a[5:0:-1]
[4, 2, 1, 4, 3]

角标为负,前角标必须大于后角标
>>> a[-2:3:1]
[]

补充:

 可变类型:列表和字典                  变:定义好的数据是否可以改变。
 不可变类型:元组,集合,字符串
   但元组特殊的一点:
        元组是不可变类型,意味着当元组中的值定义好之后,再无法修改,
		但是注意,如果元组的元素是可变类型,那么该元组是可变的!!
>>> list1 = [1,45,2,5,2,5]
>>> s1 = "ddja"
>>> t1 = (3,4,1,5,2,list1,s1)
>>> type(t1)
<class 'tuple'>
>>> t1
(3, 4, 1, 5, 2, [1, 45, 2, 5, 2, 5], 'ddja')
>>> list1[1]
45
>>> list1[1] = 7
>>> list1
[1, 7, 2, 5, 2, 5]
>>> t1
(3, 4, 1, 5, 2, [1, 7, 2, 5, 2, 5], 'ddja')
>>> t1[6]    元组也可以通过下标访问
'ddja'
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值