当前位置:网站首页>字符串常用方法
字符串常用方法
2022-08-02 02:18:00 【honker707】
作者简介:大家好我是hacker707,大家可以叫我hacker
个人主页:hacker707的csdn博客
系列专栏:python基础教程
推荐一款模拟面试、刷题神器点击跳转进入网站

字符串
字符串是什么?
字符串就是一系列字符。字符串属于不可变序列,在python中,用引号包裹的都是字符串,其中引号可以是单引号,双引号,也可以是三引号(单,双引号中的字符必须在一行,三引号中的字符可以分布在多行)
txt = 'hello world' # 使用单引号,字符串内容必须在一行
txt1 = "hello python world " # 使用双引号,字符串内容必须在一行
# 使用三引号,字符串内容可以分布在多行
txt2 = '''life is short i use python '''
字符串常用方法
1.find()
定义 find()方法返回该元素最小索引值(找不到返回-1)
举个栗子返回"python"的最小索引值
txt = "hello python world."
res = txt.find("python")
print(res)
运行结果如下:
6
2.index()
定义 index()方法返回该元素最小索引值(找不到元素会报错)
举个栗子返回"world"的最小索引值
txt = "hello python world."
res = txt.index("world")
print(res)
运行结果如下:
13
3.startswith()
定义 startswith() 方法如果字符串以指定值开头,返回True,否则返回False
举个栗子判断字符串是不是以"hello"开头
txt = "hello python world."
res = txt.startswith("hello")
print(res)
运行结果如下:
True
4.endswith()
定义 endswith() 方法如果字符串以指定值结束,返回True,否则返回False
举个栗子判断字符串是不是以"hello"结束
txt = "hello python world."
res = txt.endswith("hello")
print(res)
运行结果如下:
Flase
5.count()
定义 count() 方法返回指定值在字符串中出现的次数。
举个栗子统计"o"出现次数
txt = "hello python world."
res = txt.count("o")
print(res)
运行结果如下:
3
6.join()
定义 join() 方法获取可迭代对象中的所有项目,并将它们连接为一个字符串。必须将字符串指定为分隔符
举个栗子使用"-"作为分割符,将列表中的所有项连接到字符串中
res = ['h','e','l','l','o']
print('-'.join(res))
运行结果如下:
h-e-l-l-o
7.upper()
定义 upper()方法将字符串全部转为大写
举个栗子将字符串"hello python world"全部转为大写
tet = "hello python world"
res = txt.upper()
print(res)
运行结果如下:
HELLO WORLD
8.lower()
定义 lower()方法将字符串全部转为小写
举个栗子将字符串"HELLO PYTHON WORLD"全部转为小写
tet = "HELLO PYTHON WORLD"
res = txt.lower()
print(res)
运行结果如下:
hello python world
9.split()
定义 split()方法以指定字符分割字符串,并返回列表
举个栗子以?号作为分隔符,分割字符串
txt = "hello?python?world"
res = txt.split("?")
print(res)
运行结果如下:
['hello', 'python', 'world']
扩展分割后打印还是原字符串(字符串是不可变类型,分割操作是复制一份原字符串,更改的是复制出来的那一份)
txt = "hello?python?world"
res = txt.split("?")
# 打印分割后的
print(res)
# 打印原字符串
print(txt)
['hello', 'python', 'world']
hello?python?world
10.strip()
定义 strip()方法删除字符串两端的空格
举个栗子删除hello两端的空格
txt = " hello "
res = txt.strip()
print(res)
运行结果如下:
hello
11.replace()
定义 replace()方法以指定内容替换掉被指定内容(默认替换全部,可指定替换次数)
举个栗子以java替换python
txt = 'hello python world'
res = txt.replace('python','java')
print(res)
运行结果如下:
hello java world
扩展替换后打印还是原字符串(字符串是不可变类型,替换操作是复制一份原字符串,更改的是复制出来的那一份)
txt = 'hello python world'
res = txt.replace('python','java')
# 打印替换后的
print(res)
# 打印原字符串
print(txt)
运行结果如下:
hello java world
hello python world
12.len()
定义len()返回字符串长度
举个栗子返回’hello python world’的长度
a_str = 'hello python world'
print(len(a_str))
运行结果如下:
18
以上就是字符串常用的方法整理,如果有改进的建议欢迎私信或者在评论区留言奥~
欢迎各位来访,一起交流学习python~
边栏推荐
- "NetEase Internship" Weekly Diary (1)
- Install mysql using docker
- Garbage Collector CMS and G1
- LeetCode刷题日记:153、寻找旋转排序数组中的最小值
- 数据链路层的数据传输
- [LeetCode Daily Question] - 103. Zigzag Level Order Traversal of Binary Tree
- Redis for distributed applications in Golang
- 2022 NPDP take an examination of how the results?How to query?
- LeetCode brushing diary: 33. Search and rotate sorted array
- Safety (1)
猜你喜欢

volatile原理解析

AOF重写

The underlying data structure of Redis

AWR analysis report questions for help: How can SQL be optimized from what aspects?

MySQL8 download, start, configure, verify

FOFAHUB usage test

密码学的基础:X.690和对应的BER CER DER编码

记一次gorm事务及调试解决mysql死锁

openGauss切换后state状态显示不对
![[LeetCode Daily Question] - 103. Zigzag Level Order Traversal of Binary Tree](/img/b9/35813ae2972375fa728e3c11fab5d3.png)
[LeetCode Daily Question] - 103. Zigzag Level Order Traversal of Binary Tree
随机推荐
to-be-read list
Coding Experience Talk
2022-08-01 Reflection
LeetCode刷题日记:153、寻找旋转排序数组中的最小值
力扣、752-打开转盘锁
2022河南青训联赛第(三)场
LeetCode刷题日记:74. 搜索二维矩阵
Centos7 install postgresql and enable remote access
From 2023 onwards, these regions will be able to obtain a certificate with a score lower than 45 in the soft examination.
Oracle19c安装图文教程
【web】理解 Cookie 和 Session 机制
BioVendor Human Club Cellular Protein (CC16) Elisa Kit Research Fields
『网易实习』周记(一)
Force buckle, 752-open turntable lock
Pinduoduo leverages the consumer expo to promote the upgrading of domestic agricultural products brands and keep pace with international high-quality agricultural products
LeetCode刷题日记:34、 在排序数组中查找元素的第一个和最后一个位置
Simple example of libcurl accessing url saved as file
【LeetCode Daily Question】——704. Binary Search
MySQL - CRUD operations
十字光标太小怎么调节、CAD梦想画图算量技巧