python中fit里有字符串_Python中关于字符串你应该知道。。

# Python中字符串的常见用法

### 定义:带有双引号/单引号/三引号

### 双引号:适用于所写的字符串里没有双引号的。例如:

"凡是“辛苦”必是礼物"报错​

### 单引号:适用于所写的字符串里没有单引号的。例如:

'I'm xiaoming'报错​​

### 三引号:适用于多行字符串。例如:

''' 灯火星星...人声杳杳...歌不尽乱世烽火'''.上述三行可正常输出​​​​

###字符串可以用"" "" "" 、"str1 str2 str3"和("" "" "")以及("str1 str2 str3")表示而("","","")和 "","",""是元组

```

>>> name = "Jerry","Jenny"

>>> type(name)

>>>> name = ("as" "we" "can" "do")

>>> type(name)

>>>> poems = ("曾经沧海难为水","除去巫山不是云")​

>>> type(poems)

>​>>> name = "Jack","Keoa","Kim"

>>> name('Jack', 'Keoa', 'Kim')

>>> type(name)

>>>> name = ("as" "we" "can" "do")

>>> type(name)

>>>> name = "Jerry Jenny"

>>> name

'Jerry Jenny'

>>> type(name)

​>>>

name = ("Jerry Jack Tom")

>>> name

'Jerry Jack Tom'

>>> type(name)

```

###一些性质

####\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"

' 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 "", line 1, in AttributeError: 'str' object has no attribute 'endwith'

>>> name = "Jenny Jerry"

>>> name.endswith("rry")

True

>>> name.endswith(3) # 不支持整型

Traceback (most recent call last): File "", line 1, in 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 "", line 1, in

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 "", line 1, in 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 "", line 1, in 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'一重山一重山一重山'​​ # 输出结果

```

​#### 其他运算基本上都不能实现,比如减法,除法,乘方

欧克,先写到这里啦,如果有不对的地方,望指正呀~

知乎好像不支持Markdown,看起来又点乱,可以参考我的简书:文章正在审核中... - 简书

别忘啦关注俺滴公众号“嗅探熊”,一起发现更多黑科技~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值