【Python】5_字符串与相关方法

简介: ​一、字符串1、定义# 单引号name = "isaac"print(type(name),name)# 双引号name = "isaac"print(type(name),name)# 三引号my_str = """hello world hello python!"""print(type(my_str),my_str) # <class 'str'> hello world # hello python!my_str = '''aaa b


一、字符串
1、定义

单引号

name = "isaac"
print(type(name),name)

双引号

name = "isaac"
print(type(name),name)

三引号

my_str = """hello world

          hello python!

"""
print(type(my_str),my_str) # hello world

                        #               hello python!

my_str = '''aaa
bbb'''
print(type(my_str),my_str)

如果字符串本身包含单引号,使用双引号定义,

如果包含双引号可以使用单引号定义,或者统一使用三引号

my name is 'isaac'

my_name = "my name is 'isaac'"
print(my_name) # my name is 'isaac'

在Python中单引号和双引号的作用相同

 2、下标
支持正反下标

下标也称为是索引,是一个整型数字,可以是正数,也可以是负数

正数下标是从0开始的,表示第一个字符,

-1表示最后一个字符,倒着往前走,从后往前分别是:-1,-2,-3,-4,-5

my_str = "hello"

下标的使用语法: 变量[下标]

print(my_str[0]) # h
print(my_str[1]) # e

print(my_str[-1]) # 0
print(my_str[-3]) # l

len() 函数可以得到字符串的长度

print(len(my_str))

使用正数下标书写字符串中的最后一个元素

print(my_str[len(my_str)-1])

 3、切片

切片可以获取一段数据,多个数据,下标(索引只能获得一个数据)

切片语法:变量[start:end:step],会得到一个新的字符串

start 开始位置的下标

end 结束位置的下标,不包含end 对应的下标

step 步长,下标之间的间隔,默认为1、

my_str = "hello"
my_str1 = my_str[2:4:1] # ll 只会打出第二和三位置的字
print(my_str1)

step 如果是1,即默认值,可以不写

my_str2 = my_str[2:4]
print(my_str2) # ll

end a位置不写。表示len(),即可以取到最后一个元素

my_str3 = my_str[2:]
print(my_str3) # llo 因为len的长度大于索引的最大值(最后一个元素索引)

start 位置也可以不写,表示是0

my_str4 = my_str[:3]

start 和 end 都不写,但是冒号需要写

my_str5 = my_str[:]
print(my_str5) # hello

print(my_str[-4:-1]) # ell
print(my_str[3:1],"1") # 没有数据

步长可以是负数

print(my_str[3:1:-1],"2") # ll 2

print(my_str[::-1],) # 逆序打印 olleh

print(my_str[::-2]) # 0 2 4 hel my_str[0:5:2]

 4、字符串查找的相关方法

my_str = "hello world itcast and itcastcop"

'''
find() 在字符串中查找是否存在某个字符串
my_str.find(sub_str, start, end)
sub_str: 要在字符串中查找的内容,,类型str
start:开始的位置,从哪开始找,默认是0
end :结束的位置,默认是len()
返回值:即方法执行的结果是什么,如果找到sub_str,返回sub_str,在my_str中的位置的正数下标
如果找不到的话,就返回-1
'''

index = my_str.find("hello") # 0
print(index)

从瞎比哦啊为3的位置开始查找字符串hello

print(my_str.find("hello", 3)) # -1 说明没找到
print(my_str.find("itcast", )) # 12 说明没找到
print(my_str.find("itcast", 15)) # 23 说明没找到

rfind() right find() 从右边(后边)开始查找

print(my_str.rfind("itcast")) # 23

'''
index() 在字符串中查找是否存在某个字符串
my_str.index(sub_str, start, end)
sub_str: 要在字符串中查找的内容,,类型str
start:开始的位置,从哪开始找,默认是0
end :结束的位置,默认是len()
返回值:即方法执行的结果是什么,如果找到sub_str,返回sub_str,在my_str中的位置的正数下标
如果找不到的话,就报错(这里就是index和find之间最大的差距)
'''

print(my_str.index("hello")) # 0

print(my_str.index("hello",3)) # 没有找到的话,代码会报错

rindex() 从后边往前面找

print(my_str.rindex("itcast")) # 23
print(my_str.index("itcast")) # 12

count(sub_str,start,end) 统计出现的次数

print(my_str.count("aaaa")) # 0
print(my_str.count("hello")) # 1
print(my_str.count("itcast")) # 2
print(my_str.count("itcast", 20)) # 1

print("" 30)
print("hello " * 3) # hello hello hello

5、替换
"""
my_str.replace(old_str, new_str, count)
字符串的替换,将my_str中的old_str 替换成new_str
old_str: 将要被替换的字符串
new_str:新的字符串,替换成的字符串
count :替换的次数,默认是全部替换
返回值:得到的是一个新的字符串,不会改变原来的字符串
"""
my_str = "hello world itcast and itcastcpp"

my_str1 = my_str.replace("itcast","ithima")
print(my_str1) # hello world ithima and ithimacpp
print("my_str:", my_str ) # my_str: hello world itcast and itcastcpp

my_str2 = my_str.replace("itcast","ithima",1)
print(my_str2) # hello world ithima and itcastcpp

6、分割
'''
my_str.split(sub_str, count)
字符串的替换,将my_str 字符串按照sub_str进行切割
sub_str: 按照什么内容切割字符串,默认是空白字符,空格及tab键
count: 切割几次,默认是全部切割
返回值: 列表
相当于是把字符串切成了列表
'''
my_str = "hello world itcast and itcastcpp"

result = my_str.split() # 按照空白字符,全部切割
print(result) # ['hello', 'world', 'itcast', 'and', 'itcastcpp']

print(my_str.split("2",1)) # ['hello world itcast and itcastcpp']

这个由于未找到,所以返回的是一个完整的列表

print(my_str.split("itcast")) # ['hello world ', ' and ', 'cpp']
print(my_str.split("itcast",1)) # ['hello world ', ' and itcastcpp']
print(my_str.rsplit("itcast",1)) # ['hello world itcast and ', 'cpp']

也有rsplit

 7、join字符串的连接
'''
my_str.join(可迭代对象)
可迭代对象,str类型和列表(需要列表中的每一个数据都是字符串类型的)
将my_str 这个字符串添加到可迭代对象的两个元素之间
返回值:一个新的字符串,不会改变原字符串的值
与split方法相反,这个是把列表改成了字符串
'''
my_str = "_".join("hello")
print(my_str) # h_e_l_l_o
print("_".join("hello")) # h_e_l_l_*_o

定义列表

my_list = ["hello", "cpp", "python"]

print("_".join(my_list)) # hello_cpp_python
print("_".join(my_list)) # hello_cpp_*_python
print(" ".join(my_list)) # hello cpp python

相关文章
|
8天前
|
测试技术 API Python
【10月更文挑战第1天】python知识点100篇系列(13)-几种方法让你的电脑一直在工作
【10月更文挑战第1天】 本文介绍了如何通过Python自动操作鼠标或键盘使电脑保持活跃状态,避免自动息屏。提供了三种方法:1) 使用PyAutoGUI,通过安装pip工具并执行`pip install pyautogui`安装,利用`moveRel()`方法定时移动鼠标;2) 使用Pymouse,通过`pip install pyuserinput`安装,采用`move()`方法移动鼠标绝对位置;3) 使用PyKeyboard,同样需安装pyuserinput,模拟键盘操作。文中推荐使用PyAutoGUI,因其功能丰富且文档详尽。
|
5天前
|
机器学习/深度学习 数据采集 数据挖掘
11种经典时间序列预测方法:理论、Python实现与应用
本文将总结11种经典的时间序列预测方法,并提供它们在Python中的实现示例。
31 2
11种经典时间序列预测方法:理论、Python实现与应用
|
11天前
|
Python
【10月更文挑战第6天】「Mac上学Python 11」基础篇5 - 字符串类型详解
本篇将详细介绍Python中的字符串类型及其常见操作,包括字符串的定义、转义字符的使用、字符串的连接与格式化、字符串的重复和切片、不可变性、编码与解码以及常用内置方法等。通过本篇学习,用户将掌握字符串的操作技巧,并能灵活处理文本数据。
46 1
【10月更文挑战第6天】「Mac上学Python 11」基础篇5 - 字符串类型详解
|
1天前
|
开发者 Python
Python中的魔法方法与运算符重载
在Python的奇妙世界里,魔法方法(Magic Methods)和运算符重载(Operator Overloading)是两个强大的特性,它们允许开发者以更自然、更直观的方式操作对象。本文将深入探讨这些概念,并通过实例展示如何利用它们来增强代码的可读性和表达力。
|
9天前
|
自然语言处理 Java 数据处理
【速收藏】python字符串操作,你会几个?
【速收藏】python字符串操作,你会几个?
39 7
|
14天前
|
Python
Python中的push方法详解与实例
Python中的push方法详解与实例
15 3
|
14天前
|
存储 Python
python列表操作和方法
python列表操作和方法
15 1
|
17天前
|
存储 索引 Python
反转Python列表的4种方法
反转Python列表的4种方法
19 2
|
9天前
|
Linux Python
Python获得本机本地ip地址的方法
【10月更文挑战第8天】 socket模块包含了丰富的函数和方法,可以获取主机的ip地址,例如gethostbyname方法可以根据主机名获取ip地址,gethostbyname_ex方法可以获得本机所有ip地址列表,也可以使用netifaces模块获取网卡信息。
9 0
|
10天前
|
SQL 安全 数据库
Python防止SQL注入攻击的方法
Python防止SQL注入攻击的方法
20 0