python数据容器---字符串(str)

目录

1、字符串的定义

 2、字符串的相关操作

2.1 字符串的下标索引取值

2.2 用index方法取索引

2.3 字符串的替换

2.4 字符串的分割

2.5 字符串的规整操作

2.6 统计字符串某个字符串的出现次数

2.7 统计字符串的长度

3、字符串常用操作汇总

4、字符串的遍历

5、字符串的特点


1、字符串的定义

字符串是字符的容器,一个字符串可以存放任意数量的字符。

如:字符串:“itheima”。

 2、字符串的相关操作

2.1 字符串的下标索引取值

  • 从前向后,下标从0开始。
  • 从后向前,下标从-1开始。
my_str = "itcast and it"
# 1. 通过下标索引取值
value = my_str[0]
value2 = my_str[-1]
print(f"从字符串{my_str}取下标为0的元素。值为:{value},取下标为-1的元素。值为:{value2}")

从字符串itcast and it取下标为0的元素。值为:i,取下标为-1的元素。值为:t

同元组一样,字符串是一个:无法修改的数据容器。

2.2 用index方法取索引

# 2. 查找特定字符串的下标索引值
value = my_str.index("and")
print(f"在字符串{my_str}中查找and,其起始下标是:{value}")

在字符串itcast and it中查找and,其起始下标是:7

2.3 字符串的替换

语法:字符串.replace(字符串1,字符串2)

功能:将字符串内的全部:字符串1,替换为字符串2.

注意:不是修改字符串本身,而是得到了一个新字符串

# 3. 字符串的替换
new_my_str = my_str.replace("it","程序")
print(f"将字符串{my_str},进行替换得到:{new_my_str}")

将字符串itcast and it,进行替换得到:程序cast and 程序

2.4 字符串的分割

语法:字符串.split(分隔符字符串)

功能:按照指定的分隔符字符串,将字符串划分为多个字符串,并存入列表对象中

注意:字符串本身不变,而得到了一个列表对象

# 4. 字符串的分割
mystr = "hello python itcast"
mystr_list = mystr.split(" ")
print(f"将字符串{mystr}进行split切割得到:{mystr_list},类型是:{type(mystr_list)}")

将字符串hello python itcast进行split切割得到:['hello', 'python', 'itcast'],类型是:<class 'list'>

2.5 字符串的规整操作

语法1:字符串.strip()   #(不传入参数去前后空格)

# 5. 字符串的规整操作(取前后空格)
mystr = "    hello python itcast     "
new_str = mystr.strip()
print(f"字符串{mystr}被strip后,结果:{new_str}")

字符串    hello python itcast     被strip后,结果:hello python itcast

语法2:字符串.strip(字符串)    #(去前后指定字符串)

# 5. 字符串取除操作strip
mystr = "12hello python itcast21"
new_str = mystr.strip("12")
print(f"字符串{mystr}被strip后,结果:{new_str}")

字符串12hello python itcast21被strip后,结果:hello python itcast

注意:传入的是“12” 其实就是:“1”和“2”都会移除,是按个单个字符。

2.6 统计字符串某个字符串的出现次数

# 6. 统计字符串某个字符串的出现次数
mystr = "itcast and it"
count = mystr.count("it")
print(f"字符串{mystr}it出现的次数:{count}")

字符串itcast and it,it出现的次数:2

2.7 统计字符串的长度

# 7. 统计字符串的长度
num = len(mystr)
print(f"字符串{mystr}的长度是,{num}")

字符串itcast and it的长度是,13

3、字符串常用操作汇总

操作说明
字符串[下标]根据下标索引取出特定位置字符
字符串.index(字符串)查找给定字符串的第一个匹配的下标
字符串.replace(字符串1,字符串2)将字符串1 替换为 字符串2       
字符串.split(字符串)按照指定的分隔符字符串,将字符串划分为多个字符串,并存入列表对象中,得到的是一个列表

字符串.strip()

字符串.strip(字符串)

移除首尾的空格和换行符或指定字符串
字符串.count统计字符串内某个字符串的出现次数
len(字符串)统计字符串的字符个数

4、字符串的遍历

同列表,元组一样也支持while,for循环进行遍历。

5、字符串的特点

  • 只可以存储字符串
  • 支持下标索引
  • 允许重复字符串存在
  • 不可修改

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
链表可以通过自定义容器类来进行容器化。以下是一个简单的示例: ```python class Node: def __init__(self, data=None): self.data = data self.next = None class LinkedList: def __init__(self): self.head = None def append(self, data): new_node = Node(data) if self.head is None: self.head = new_node return last_node = self.head while last_node.next is not None: last_node = last_node.next last_node.next = new_node def __len__(self): current_node = self.head count = 0 while current_node: count += 1 current_node = current_node.next return count def __getitem__(self, index): if index < 0 or index >= len(self): raise IndexError("Index out of range") current_node = self.head for i in range(index): current_node = current_node.next return current_node.data def __setitem__(self, index, data): if index < 0 or index >= len(self): raise IndexError("Index out of range") current_node = self.head for i in range(index): current_node = current_node.next current_node.data = data def __str__(self): current_node = self.head nodes = [] while current_node: nodes.append(str(current_node.data)) current_node = current_node.next return " -> ".join(nodes) ``` 在这个示例中,我们定义了两个类:`Node` 和 `LinkedList`。`Node` 类代表链表中的每个节点,`LinkedList` 类代表整个链表。 `Node` 类有一个 `data` 属性,用于存储节点的数据,还有一个 `next` 属性,用于指向下一个节点。`LinkedList` 类有一个 `head` 属性,用于指向链表的头部。 `LinkedList` 类有一些方法,包括 `append`、`__len__`、`__getitem__`、`__setitem__` 和 `__str__`。其中,`append` 用于向链表末尾添加一个节点,`__len__` 用于返回链表中节点的数量,`__getitem__` 和 `__setitem__` 用于按索引获取和设置节点的数据,`__str__` 用于返回链表的字符串表示形式。 使用这个链表容器,你可以创建一个链表并向其中添加元素,然后像操作列表一样访问和修改链表中的元素。例如: ```python my_list = LinkedList() my_list.append(1) my_list.append(2) my_list.append(3) print(len(my_list)) # 输出 3 print(my_list[1]) # 输出 2 my_list[2] = 4 print(my_list) # 输出 "1 -> 2 -> 4" ```

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

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值