Python数据容器之str(字符串)

178 阅读3分钟
数据容器——str(字符串)
字符串在Python中的多种定义形式

1.单引号定义法:name = '黑马'

2.双引号定义法:name = "黑马"

3.三引号定义法:name = """黑马"""

三引号注释法和多行注释的写法一样,同时也支持换行书写;

当使用变量接收时,就是字符串

不使用变量接收时,就是多行注释

如果字符串中包含双引号:

name = '"黑马"'

如果字符串中包含单引号:

name = "'黑马'"

或者使用转义字符:

name = "\"黑马\""
name = '\'黑马\''
字符串的拼接
  • "+"

注意:”+“操作只能拼接字符串本身,不能拼接字符串以外的其他类型

  • 字符串格式化:%占位(本质也是字符串的拼接)

常用数据类型占位:%s(字符串) %d(整数) %f(浮点数)

格式化的精度控制:

使用辅助符号”m.n“来控制数据的宽度和精度

m,控制宽度,要求是数字(很少使用)设置的宽度小于数字自身,不生效

.n,控制小数点精度,要求是数字,会进行小数的四舍五入

表达式的格式化:

print("3 * 4 = %s" % (3 * 4))
print("字符串的类型为:%s" % type("字符串"))
  • 快速字符串格式化:f"字符串{变量}"
name = "传智播客"
set_up_year = 2006
stock_price = 19.99
print(f"{name}, 成立于{set_up_year},股价为:{stock_price}")

不理会类型,不做精度控制。

字符串的操作

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

字符串这种数据容器只可以存储字符串。

str下标(索引)——查元素
# 通过下标索引,获取特定位置的字符
my_str = "itheima and itcast"
value_1 = my_str[2]
print(value_1) # h
value_2 = my_str[-16]
print(value_2) # h

同元组一样,字符串是不可修改的数据容器。(只读)

修改指定下标的字符串(my_str[0] = 'a')

移除特定下标字符串(del my_str[0]\my_str.remove()\my_str.pop)

追加字符串(my_str.append())

均无法完成,如果必须要做,只能得到一个新的字符串,旧的字符串无法修改。

字符串的常用操作
# 查找特定字符串的下标索引值
# 语法:字符串.index(字符串)
my_str = "itheima and itcast"
index = my_str.index("and")
print(index) # 8# 字符串的替换
# 语法:字符串.replace(字符串1, 字符串2)
# 功能:将字符串内的全部字符串1替换为字符串2
# 注意:不是修改字符串本身,而是得到一个新的字符串(有返回值)
my_str = "itheima and itcast"
my_str_2 = my_str.replace("and", "&")
print(my_str) # itheima and itcast
print(my_str_2) # itheima & itcast# 字符串的分割
# 语法:字符串.split(分割字符串)
# 功能:按照指定的分隔符字符串,将字符串划分为多个字符串,并存入列表对象中
# 注意:字符串本身不变,而是得到了一个列表对象(有返回值)
my_str = "itheima and itcast"
my_str_list = my_str.split(" ")
print(my_str) # itheima and itcast
print(my_str_list) # ['itheima', 'and', 'itcast']
print(type(my_str_list)) # <class 'list'># 字符串的规整操作(去前后空格)
# 语法:字符串.strip()
my_str = "   itheima and itcast   "
my_str_2 = my_str.strip()
print(my_str_2) # itheima and itcast# 字符串的规整操作(去前后指定字符串)
# 语法:字符串.strip(字符串) # 该函数形参有默认值,因此可传参可不传参
# 形参中包含的字符串可看作是多个单个字符,去除时不分排列顺序,均会去除
my_str = "sunitheima and itcastsunsunsun"
my_str_2 = my_str.strip("sun")
print(my_str_2) # itheima and itcastmy_str = "uuunsunitheima and itcastsusnunun"
my_str_3 = my_str.strip("sun")
print(my_str_3) # itheima and itcast# 统计字符串中某个字符出现的次数
# 语法:字符串.count(字符串)
my_str = "itheima and itcast"
count = my_str.count("it")
print(count) # 2# 统计字符串的长度
# 语法:len(字符串)
my_str = "itheima and itcast"
lenth = len(my_str)
print(lenth) # 18
字符串的遍历
my_str = "itheima and itcast"index = 0
while index < len(my_str):
    print(my_str[index])
    index += 1
print()
print()
for element in my_str:
    print(element)
练习
my_str = "itheima and itcast"
​
it_count = my_str.count("it")
print(it_count) # 2
​
my_new_str = my_str.replace(" ", "|")
print(my_new_str) # itheima|and|itcast
​
my_new_str_list = my_new_str.split("|")
print(my_new_str_list) # ['itheima', 'and', 'itcast']# 延申
my_str = str()
print(my_str) # 
print(type(my_str)) # <class 'str'>
print(len(my_str)) # 0
​
my_str = str("hello")
print(my_str) # hello
print(type(my_str)) # <class 'str'>
print(len(my_str)) # 5