Python中关于字符串你应该知道这些...
# Python中字符串的常见用法
### 定义:带有双引号/单引号/三引号
### 双引号:适用于所写的字符串里没有双引号的。例如:
"凡是“辛苦”必是礼物"报错
### 单引号:适用于所写的字符串里没有单引号的。例如:
'I'm xiaoming'报错
### 三引号:适用于多行字符串。例如:
''' 灯火星星...人声杳杳...歌不尽乱世烽火'''.上述三行可正常输出
###字符串可以用"" "" "" 、"str1 str2 str3"和("" "" "")以及("str1 str2 str3")表示而("","","")和 "","",""是元组
```
>>> name = "Jerry","Jenny"
>>> type(name)
<class 'tuple'
>>>> name = ("as" "we" "can" "do")
>>> type(name)
<class 'str'
>>>> poems = ("曾经沧海难为水","除去巫山不是云")
>>> type(poems)
<class 'tuple'
>>>> name = "Jack","Keoa","Kim"
>>> name('Jack', 'Keoa', 'Kim')
>>> type(name)
<class 'tuple'
>>>> name = ("as" "we" "can" "do")
>>> type(name)
<class 'str'
>>>> name = "Jerry Jenny"
>>> name
'Jerry Jenny'
>>> type(name)
<class 'str'>>>>
name = ("Jerry Jack Tom")
>>> name
'Jerry Jack Tom'
>>> type(name)
<class 'str'>
```
###一些性质
####\t \n \t\n
#####\t: \t表示空四个字符,也称缩进,相当于按一下Tab键
```
>>> name = "\tJerry Jenny"
>>> print(name)
Jerry Jenny
>>> name = "Jerry\tJenny"
>>> print(name)
Jerry Jenny
>>> name = "Jer\try Jen\tny"
>>> print(name)
Jer ry Jen ny
```
##### \n : \n表示换行,相当于按一下回车
```
>>> name = "Jerry Jenny\n"
>>> print(name)
Jerry Jenny
>>> name = "Jerry\nJenny"
>>> print(name)
Jerry
Jenny
>>> name = "Jerry\nJenn\ny"
>>> print(name)
Jerry
Jenny
```
##### \t\n: \n\t表示换行加每行空四格
```
>>> name = "\tJerry Jenny\n"
>>> print(name)
Jerry Jenny
>>> name = "\tJerry\nJenny"
>>> print(name)
Jerry
Jenny
>>> name = "\t\nJjerry Jenny"
>>> print(name)
Jjerry Jenny
>>> name = "Jerry Jenny\t\n"
>>> print(name)
Jerry Jenny
```
##### .center(number," ")以number个居中对齐
```
>>> name = "Jerry Tom"
>>> name.center(50)
' Jerry Tom '
>>> name.center(50,"-")
'--------------------Jerry Tom---------------------'
```
##### .count(" ",start,end) 找指定字符串在star到end范围内出现的次数
```
>>> name = "Jenny Jerry"
>>> name.count("J")
2
>>> name.count("J",1,7) # 一个空格占一个下标
1
>>> name.count("J",1,6) # 顾头不顾尾
0
>>> name[6]
'J'
>>> name.count("nn")
1
```
##### .endswith("") 检查字符串最后是不是以指定字符结尾的
```
>>> name = "Jenny Jerry"
>>> name.endwith("rry") # 是.endswith而不是endwith哦
Traceback (most recent call last): File "<stdin>", line 1, in <module>AttributeError: 'str' object has no attribute 'endwith'
>>> name = "Jenny Jerry"
>>> name.endswith("rry")
True
>>> name.endswith(3) # 不支持整型
Traceback (most recent call last): File "<stdin>", line 1, in <module>TypeError: endswith first arg must be str or a tuple of str, not int
>>> name.endswith("3")
False
>>> name.endswith("y Jerry")
True
```
##### .startswith("") 检查字符串湿奴是以指定字符开头的
```
>>> name = "Jenny Jerry"
>>> name.startswith("Je")
True
>>> name.startswith("e")
False
```
##### .find("",start,end),同样是顾头不顾尾,在字符串内存在指定字符则返回第一个出现的下标,不存在则返回-1
```
>>> name = "Jerry Jenny"
>>> name.find("J")
0
>>> name.find("J",1)
6
>>> name.find("J",1,7)
6
>>> name.find("J",1,6)
-1 # 顾头不顾尾检索不到6,故不存在返回-1
>>> name.find("m")
-1
```
##### .format() 格式化打印
```
>>> a = "my name is %s,i am %s years old" %("Jerry",20)
>>> a
'my name is Jerry,i am 20 years old'
>>> a = "my name is %s,i am %d years old" %("Jerry",20)
>>> a'
my name is Jerry,i am 20 years old'
>>> a = "my name is {0},i am {1} years old"
>>> a.format("Jerry",20) # 直接打印出来不用再调用a或者print
'my name is Jerry,i am 20 years old'
>>> a = "my name is {name},i am {age} years old"
>>> a.format(name ="Jerry",age=20)'
my name is Jerry,i am 20 years old'
>>> a = "she is {name},{name} is {age} years old"
>>> a.format(name="Jerry",age=20)
'she is Jerry,Jerry is 20 years old'
```
##### .isdigit() 判断是不是整数
```
>>> name = "Jerry"
>>> name.isdigit()
False
>>> age = "12"
>>> age.isdigit()
True
>>> a = "Jerry,20"
>>> a.isdigit()
False
>>> b="12"
>>> b.isdigit()
True
>>> b = "20"+"2"
>>> b
'202'
>>> b.isdigit()
True
>>> b = "20 12"
>>> b.isdigit()
False
>>> "20".isdigit()
True
>>> "20 1".isdigit()
False
>>> a = "20 1"
>>> a
'20 1'
>>> a = 21
>>> a.isdigit()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'int' object has no attribute 'isdigit'
```
##### .capitalize() 首字母大写
```
>>> name = "jerry amy"
>>> name.capitalize()
'Jerry amy'
>>> name = "Jerry amy"
>>> name.capitalize()
'Jerry amy'
```
##### .index("",start,end) 索引找出指定元素的第一个下标
```
>>> name = "Jerry"
>>> name.index("J")
0
>>> name.index("r")
2
>>> name.index("r",3,4)
3
```
##### .casefold() 大写全部变为小写
```
>>> name = "Jerry Tom"
>>> name'Jerry Tom'
>>> name.casefold()
'jerry tom'
```
##### .lower()全部变小写
```
>>> name ="Jerry Jenny"
>>> name.lower()
'jerry jenny'
>>> name = "jerry jenny"
>>> name.lower()
'jerry jenny'
```
##### .upper()全部变大写
```
>>> name = "Jenny Jerry"
>>> name.upper()
'JENNY JERRY'
>>> name = "Jenny-Jerry"
>>> name.upper()
'JENNY-JERRY'
```
##### .islower()判断出现字符的地方是不是全都是小写,如果没有字符,比如数字或特殊形式也返回False
```
>>> name ="Jerry Jenny"
>>> name.islower()
False
>>> name = "jerry jenny"
>>> name.islower()
True
>>> name = "13 jerry"
>>> name.islower()
True
>>> age = "20 12"
>>> age.islower()
False
>>> sign = "* & $"
>>> sign.islower()
False
```
##### .isupper()判断出现字符的地方是不是全都是大写,如果没有字符,比如数字或特殊形式也返回False
```
>>> name = "Jerry Jenny"
>>> name.isupper()
False
>>> name = "JERRY JENNY"
>>> name.isupper()
True
>>> name = "12"
>>> name.isupper()
False
>>> name = "JERRY 12"
>>> name.isupper()
True
>>> name = "JENNY 12t"
>>> name.isupper()
False
>>> poems = "曾经沧海难为水,除去巫山不是云"
>>> name.isupper()
False
>>> poems = "曾经沧海难为水,除去巫山不是云A"
>>> poems.isupper()
True
>>> poems = "曾经沧海难为水,除去巫山不是云,A"
>>> poems.isupper()
True
```
##### .swapcase()把字符串里的大写变小写,小写变大写
```
>>> name = "Jenny Jerry"
>>> name.swapcase()
'jENNY jERRY'
>>> poems = "曾经沧海难为水,除去巫山不是云"
>>> poems.swapcase()
'曾经沧海难为水,除去巫山不是云'
```
##### .isspace()判断有没有空间
```
>>> a =''
>>> a.isspace() # 仅有引号不算 必须只有空格
False
>>> a = ' '
>>> a.isspace()
True
>>> a = ' '
>>> a.isspace()
True>
>> a = '1 2 3'
>>> a
'1 2 3'
>>> a.isspace()
False
```
##### "".join()把列表或者字符串里面的字符以“”里面的形式拼接起来
```
>>> numbers = [1,2,4]
>>> "-".join(numbers)
Traceback (most recent call last): File "<stdin>", line 1, in <module>TypeError: sequence item 0: expected str instance, int found
>>> str = ["1","2","4"]
>>> "-".join(str)'
1-2-4'
>>> name = ("Jerry" "Jenny" "Alex")
>>> "*".join(name)
'J*e*r*r*y*J*e*n*n*y*A*l*e*x'
>>> "-".join(name)
'J-e-r-r-y-J-e-n-n-y-A-l-e-x'
>>> "".join(name)'
JerryJennyAlex'
>>> name = ("Jerry","Jenny","Alex")
>>> "-".join(name)
'Jerry-Jenny-Alex'
```
##### .ljust(step,"sign") 从左对齐step个不够的用sign补齐
```
>>> poems = ("曾经沧海难为水" "除去巫山不是云")
>>> (poems).ljust(50,"_")
'曾经沧海难为水除去巫山不是云____________________________________'
>>> poems.ljust(50,"-")
'曾经沧海难为水除去巫山不是云------------------------------------'
```
##### .rjust(step,"sign")从右对齐step个不够的用sign补齐
```
>>> poems.rjust(50,"*")
'************************************曾经沧海难为水除去巫山不是云'>>> poems.rjust(50,"")
Traceback (most recent call last): File "<stdin>", line 1, in <module>TypeError: The fill character must be exactly one character long
>>> poems.rjust(50," ")
' 曾经沧海难为水除去巫山不是云'
```
##### .zfill(step)从左对齐step个不够的用0补齐
```
>>> poems = "曾经沧海难为水,除去巫山不是云"
>>> poems.zfill(50)
'00000000000000000000000000000000000曾经沧海难为水,除去巫山不是云'
```
##### .strip() 清楚两边格式 即将字符串里两边的换行缩进都清除 如果在中间就清除不了
```
>>> name = "Jerry Jenny\t\n"
>>> print(name)
Jerry Jenny
>>> name.strip()
'Jerry Jenny'
>>> name = "Jenny\tJerry\n"
>>> print(name)
Jenny Jerry
>>> name.strip()
'Jenny\tJerry'
>>> name = " Jerry-Jenny \t Jack\n"
>>> print(name)
Jerry-Jenny Jack
>>> name.strip()
'Jerry-Jenny \t Jack'
>>> name = "------------- Jerry\nJenny "
>>> print(name)
------------- JerryJenny
>>> name.strip()
'------------- Jerry\nJenny'
```
##### .lstrip() 清楚左边格式 即将字符串左边的换行缩进都清除 如果在中间和右边就清除不了
```
>>> name = "\t\nJjerry Jenny"
>>> print(name)
Jjerry Jenny
>>> name.lstrip()
'Jjerry Jenny'
>>> name = "Jerry Jenny\t\n"
>>> name.lstrip()
'Jerry Jenny\t\n'
```
##### .rstrip() 清楚右边格式 即将字符串右边的换行缩进都清除 如果在中间和左边就清除不了
```
>>> name = "Jerry Jenny\t\n"
>>> name.rstrip()
'Jerry Jenny'
>>> name = "\t\nJjerry Jenny"
>>> name.rstrip()
'\t\nJjerry Jenny'
```
##### .replace("str1","str2",count)把字符串里的str1改count次str2
```
>>> a = "I am 20 years old,and Amy is 20 years old too"
>>> a.replace("20","21")
'I am 21 years old,and Amy is 21 years old too'
>>> a.replace("20","21",1)
'I am 21 years old,and Amy is 20 years old too'
```
##### .split("",count) 通过指定分隔符对字符串进行切片,count默认=-1,即全分
```
>>> name = "Jerry Jenny Jack"
>>> name.split()
['Jerry', 'Jenny', 'Jack'] # 默认count=-1,即全分
>>> name.split(" ",1)
['Jerry', 'Jenny Jack']
>>> name = "Jerry,Jenny,Jack"
>>> name.split()
['Jerry,Jenny,Jack']
>>> name.split(",",1) # 只分一次,从左算起
['Jerry', 'Jenny,Jack']
>>> name = "Jerry-Jenny#Jack"
>>> name
'Jerry-Jenny#Jack'
>>> name.split()
['Jerry-Jenny#Jack']
>>> name.split("-") # 依据-开始分
['Jerry', 'Jenny#Jack']
>>> name.split("#",2)
['Jerry-Jenny', 'Jack']
```
##### .rsplit("",count)和.split()用法基本一样,只是从右开始分,count=-1时看不出来区别
```
>>> name = "Jerry-Jenny#Jack"
>>> name
'Jerry-Jenny#Jack'
>>> name.rsplit()
['Jerry-Jenny#Jack']
>>> name.rsplit("-")
['Jerry', 'Jenny#Jack']
>>> name.rsplit("#")
['Jerry-Jenny', 'Jack']
>>> name = "*Jack*Jerry#Jenny"
>>> name.rsplit("*",1) # 从右开始按*分一次
['*Jack', 'Jerry#Jenny']
>>> name.rsplit("*") # 从右开始按*全分,从又开始算起相当于第一个*号前面是个空格
['', 'Jack', 'Jerry#Jenny']
>>> name.split("*",1) # 从左开始按*分一次
['', 'Jack*Jerry#Jenny']
>>> name.split("*")
['', 'Jack', 'Jerry#Jenny']
```
#### 拼接:可进行字符串的加,乘,例如:
```
str1="一重山"
str2="两重山"
str1+str2
'一重山两重山' # 输出结果
str1*3'一重山一重山一重山' # 输出结果
```
#### 其他运算基本上都不能实现,比如减法,除法,乘方
欧克,先写到这里啦,如果有不对的地方,望指正呀~
wuwuwu好像不支持Markdown,看起来有点乱,可以参看我的CSDN:https://blog.csdn.net/m0_46252978/article/details/105568531
或者简书:https://www.jianshu.com/p/90280e7f8ab7
Python中关于字符串你应该知道这些...的更多相关文章
- Python中Unicode字符串
Python中Unicode字符串 字符串还有一个编码问题. 因为计算机只能处理数字,如果要处理文本,就必须先把文本转换为数字才能处理.最早的计算机在设计时采用8个比特(bit)作为一个字节(byte ...
- Python中的字符串处理
Python转义字符 在需要在字符中使用特殊字符时,python用反斜杠(\)转义字符.如下表: 转义字符 描述 \(在行尾时) 续行符 \\ 反斜杠符号 \' 单引号 \" 双引号 \a ...
- python中修改字符串的几种方法
在Python中,字符串是不可变类型,即无法直接修改字符串的某一位字符.因此改变一个字符串的元素需要新建一个新的字符串.常见的修改方法有以下4种. 方法1:将字符串转换成列表后修改值,然后用join组 ...
- python中根据字符串导入模块module
python中根据字符串导入模块module 需要导入importlib,使用其中的import_module方法 import importlib modname = 'datetime' date ...
- python中的字符串
一.在python中,字符串是不可变类型 通过以下代码说明: >>> s = 'hello, world' >>> id(s) 2108634288304 > ...
- 【转】Python中的字符串与字符编码
[转]Python中的字符串与字符编码 本节内容: 前言 相关概念 Python中的默认编码 Python2与Python3中对字符串的支持 字符编码转换 一.前言 Python中的字符编码是个老生常 ...
- Python中常见字符串去除空格的方法总结
Python中常见字符串去除空格的方法总结 1:strip()方法,去除字符串开头或者结尾的空格>>> a = " a b c ">>> a.s ...
- Python中的字符串方法
Python中的字符串方法 字符串类即str提供了许多有用的方法来操纵字符串.具体来说,我们将讨论如下的方法. 搜索字符串内的子字符串. 测试字符串. 格式字符串. 转换字符串. 回顾前面的章节,方法 ...
- python中的字符串切片
python中的字符串切片,似乎有点乱,例如: >>>pystr='Python' >>>pystr[2:5] 就会输出 'tho' 这该怎样理解呢?中括号[2:5 ...
- python 中的字符串格式化
python 中的字符串格式化 %方式的调用 1.格式化代码 代码 意义 s 字符串,使用str r 字符串,使用repr不使用str c 字符 d 十进制的数字 i 整数 u 无符号整数 o 八进制 ...
随机推荐
- Java 锁详解(转)
转自 https://www.cnblogs.com/jyroy/p/11365935.html Java提供了种类丰富的锁,每种锁因其特性的不同,在适当的场景下能够展现出非常高的效率.本文旨在对锁相 ...
- JDK14发布了,快来看看有什么新特性吧
序言
- c# winform 访问WebServices 服务(通过WEB引用的方式进行访问)
第一步.Winform项目引用WEB服务 第二步.代码声明实例化 Web引用 YzServ.TestServ yzserv = new WebYzServ.TestServ(); yzserv.AAA ...
- [Docker03] Deploy LNMP on Docker
Deploy MYSQL docker pull mysql 挂载卷保存数据文件 mkdir -p /mysql/data chmod -p 777 /mysql/data MySQL使用过程中的环境 ...
- JDBC(二)
## 详解JDBC各个对象 1.DriverManager对象:驱动管理对象 * 功能: 1.注册驱动:告诉程序该使用哪个数据jar包 static void registerDriver(D ...
- Linux基础篇学习——Linux文件系统之文件存储与读取:inode,block,superblock
Linux文件类型 代表符号 含义 - 常规文件,即file d directory,目录文件 b block device,块设备文件,支持以"block"为单位进行随机访问 c ...
- 文件映射(Windows核心编程)
映射内存的可执行文件和dll 当一个线程调用CreateProcess的时候,系统会执行以下步骤: 系统会先确定CreateProcess所指定的可执行文件的所在位置.如果找不到文件,那么Create ...
- Mybatis详解系列(一)--持久层框架解决了什么及如何使用Mybatis
简介 Mybatis 是一个持久层框架,它对 JDBC 进行了高级封装,使我们的代码中不会出现任何的 JDBC 代码,另外,它还通过 xml 或注解的方式将 sql 从 DAO/Repository ...
- [SQL]511+512+534+550+569
511. 游戏玩法分析 I solution SELECT player_id, MIN(event_date) AS first_login FROM Activity GROUP BY playe ...
- 5分钟配置好你的AI开发环境
作者 | Revolver 无论是第一次设置TensorFlow的新手数据科学爱好者,还是使用TB级数据的经验丰富的AI工程师,安装库.软件包或者框架总是一个困难又繁琐的过程.但是像Docker这样的 ...