# 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中关于字符串你应该知道这些...的更多相关文章

  1. Python中Unicode字符串

    Python中Unicode字符串 字符串还有一个编码问题. 因为计算机只能处理数字,如果要处理文本,就必须先把文本转换为数字才能处理.最早的计算机在设计时采用8个比特(bit)作为一个字节(byte ...

  2. Python中的字符串处理

    Python转义字符 在需要在字符中使用特殊字符时,python用反斜杠(\)转义字符.如下表: 转义字符 描述 \(在行尾时) 续行符 \\ 反斜杠符号 \' 单引号 \" 双引号 \a ...

  3. python中修改字符串的几种方法

    在Python中,字符串是不可变类型,即无法直接修改字符串的某一位字符.因此改变一个字符串的元素需要新建一个新的字符串.常见的修改方法有以下4种. 方法1:将字符串转换成列表后修改值,然后用join组 ...

  4. python中根据字符串导入模块module

    python中根据字符串导入模块module 需要导入importlib,使用其中的import_module方法 import importlib modname = 'datetime' date ...

  5. python中的字符串

    一.在python中,字符串是不可变类型 通过以下代码说明: >>> s = 'hello, world' >>> id(s) 2108634288304 > ...

  6. 【转】Python中的字符串与字符编码

    [转]Python中的字符串与字符编码 本节内容: 前言 相关概念 Python中的默认编码 Python2与Python3中对字符串的支持 字符编码转换 一.前言 Python中的字符编码是个老生常 ...

  7. Python中常见字符串去除空格的方法总结

    Python中常见字符串去除空格的方法总结 1:strip()方法,去除字符串开头或者结尾的空格>>> a = " a b c ">>> a.s ...

  8. Python中的字符串方法

    Python中的字符串方法 字符串类即str提供了许多有用的方法来操纵字符串.具体来说,我们将讨论如下的方法. 搜索字符串内的子字符串. 测试字符串. 格式字符串. 转换字符串. 回顾前面的章节,方法 ...

  9. python中的字符串切片

    python中的字符串切片,似乎有点乱,例如: >>>pystr='Python' >>>pystr[2:5] 就会输出 'tho' 这该怎样理解呢?中括号[2:5 ...

  10. python 中的字符串格式化

    python 中的字符串格式化 %方式的调用 1.格式化代码 代码 意义 s 字符串,使用str r 字符串,使用repr不使用str c 字符 d 十进制的数字 i 整数 u 无符号整数 o 八进制 ...

随机推荐

  1. linux无文件执行— fexecve 揭秘

    前言 良好的习惯是人生产生复利的有力助手. 继续2020年的flag,至少每周更一篇文章. 无文件执行 之前的文章中,我们讲到了无文件执行的方法以及混淆进程参数的方法,今天我们继续讲解一种linux上 ...

  2. Journal of Proteome Research | Lipidomics reveals similar changes in serum phospholipid signatures of overweight and obese paediatric subjects (分享人:赵倩倩)

    文献名:Lipidomics reveals similar changes in serum phospholipid signatures of overweight and obese paed ...

  3. VS code调试代码快速上手必备知识

    一.通过简单的配置运行一个JavaScript程序 1.打开(创建)一个新的工作空间(文件夹),并创建一个js文件: var name='world'; var s='Hello,${name}!'; ...

  4. Django 后台管理xadmin

    一. xadmin的使用 后台管理在开发中可以给我们提供很大的便利,django自带了一个后台管理admin,不过还有一个xadmin比django自带的好用一些,功能更加强大,为模型提供了版本控制, ...

  5. input标签的accept属性、JQuery绑定keyDown事件

    一. input标签的accept属性 当我们上传文件或者注册上传头像时,我们可以一般都是使用: <input type="file" id="my_file&qu ...

  6. 【2020-03-28】Dubbo源码杂谈

    前言 本周空闲时间利用了百分之六七十的样子.主要将Dubbo官网文档和本地代码debug结合起来学习,基本看完了服务导出.服务引入以及服务调用的过程,暂未涉及路由.字典等功能.下面对这一周的收获进行一 ...

  7. 网维大师无盘刷新B盘方法

  8. 详解firewalld 和iptables

    在RHEL7里有几种防火墙共存:firewalld.iptables.ebtables,默认是使用firewalld来管理netfilter子系统,不过底层调用的命令仍然是iptables等. fir ...

  9. test命令的使用以及判断语法

    test命令 Shell中的 test 命令用于检查某个条件是否成立,它可以进行数值.字符和文件三个方面的测试. 语法:test EXPRESSION 或者 [ EXPRESSION ] 字符串判断( ...

  10. python课程体系是怎么样的?

    好的python课程体系是怎么样的?Python从1991年走到今天,已经有了28年的历史了,在开发行业来说也是老江湖了,那么python为什么可以在开发行业屹立不倒呢?其实python最吸引程序员的 ...