python内置函数str的作用_Python数据类型的内置函数之str(字符串)

Python数据类型内置函数

- str(字符串)

- list(列表)

- tuple(元组)

- dict(字典)

- set(收集)

str(字符串)的一些操作

- 字符串相连方法

1 #字符串的相连

2 str_1 = "I am"

3 str_2 = "string practice"

4

5 print(str_1 +str_2)6 #执行的结果

7 I amstring practice8 #可以在中间用空格隔开

9 print(str_1 + " " +str_2)10 #执行的结果

11 I am string practice12

13 #使用%s的方法相连

14 str_3 = "We let's"

15 str_4 = "keep practing"

16

17 print("%s %s" %(str_3,str_4))18 #执行的结果

19 We let's keep practing

20

21 #使用format的方法相连

22 str_5 = "See final"

23 str_6 = "practcie"

24

25 print("{} {}".format(str_5,str_6))26 #执行的结果

27 See final practcie

- 字符串的乘法(你特别想多打印几遍这句话可以使用*)

1 #字符串打印多遍

2 rst_1 = "Hello World"

3 rst_2 = rst_1 * 5

4

5 print(rst_2)6 #执行的结果

7 Hello World Hello World Hello World Hello World Hello World

- 字符串变成列表使用

1 #字符串的列表使用

2 s = "I need help"

3

4 print(s[0])5 #执行的结果

6 I7

8 #进行分片操作

9 print(s[2:11])10 #执行的结果

11 need help

- (capitalize)首字母大写其余小写,返回字符串

1 #首字母大写其余小写,返回字符串

2 st_1 = "i need practice"

3 st_2 =st_1.capitalize()4

5 print(st_2)6 #执行的结果

7 I need practice

- (title)每个单词首字母改变为大写,返回字符串

1 #将每个单词首字母变成大写,返回字符串

2 st_1 = "i need practice"

3 st_2 =st_1.title()4

5 print(st_2)6 #执行结果

7 I Need Practice

- (upper)所有字母都改变为大写,返回字符串

1 #将所有字母变成大写,返回字符串

2 st_1 = "i need practice"

3 st_2 =st_1.upper()4

5 print(st_2)6 #执行结果

7 I NEED PRACTICE

- (lower)所有字母都改变为小写,返回字符串

1 #将所有字母改变为小写,返回字符串

2 st_1 = "I NEED PRACTICE"

3 st_2 =st_1.lower()4

5 print(st_2)6 #执行结果

7 i need practice

- (swapcase)所有字母转化大小写,返回字符串

1 #将所有字母大小写互换,返回字符串

2 st_1 = "i NEED pratice"

3 st_2 =st_1.swapcase()4

5 print(st_2)6 #执行的结果

7 I need PRATICE

- (len)测量字符串长度

1 #测量字符的总长度

2 #len统计的长度是按字符,不是内存的长度

3 st_1 = "i need pratice"

4 st_2 =len(st_1)5

6 print(st_2)7 #执行结果

8 14

- (is)检查字母的格式

1 #(isupper)检查指定字母是否都为大写,返回布尔值

2 st_1 = "We Are Practice"

3 st_2 = "WE ARE PRACRICE"

4 print(st_1.isupper())5 #执行结果

6 False7 print(st_2.isupper())8 #执行结果

9 Ture10

11 #(islower)检查指定字母是否为小写,返回布尔值

12 st_1 = "We Are Practice"

13 st_2 = "we are practice"

14 print(st_1.islower())15 #执行结果

16 False17 print(st_2.islower())18 #执行结果

19 Ture20

21 #(title)检查指定每个单词首字母是否为大写,返回布尔值

22 st_1 = "we are practice"

23 st_2 = "We Are Practice"

24 print(st_1.istitle())25 #执行结果

26 False27 print(st_2.istitle())28 #执行结果

29 Ture30

31 #(isspace)检查是否为空字符串

32 st_1 = "We are"

33 st_2 = " "

34 print(st_1.isspace())35 #执行结果

36 False37 print(st_2.isspace())38 Ture

- (find)(index)查找字符串的索引

1 #查找字符串的索引

2 #使用find查找

3 stf_1 = "Stare at me"

4 stf_2 = sti_1.find("r")5 print(sti_2)6 #执行结果

7 10

8

9 #可以查找你想要搜索的区域

10 stf_3 = stf_1.find("t",3,10)11 print(stf_3)12 #执行结果

13 7

14 #使用index查找

15 sti_1 = "Let's go on"

16 sti_2 = sti_1.index("n")17 print(sti_2)18 #执行结果

19 10

20

21 #可以查找你想要搜索的区域

22 sti_3 = sti_1.index("s",2,8)23 print(sti_3)24 #执行结果

25 4

- (count)统计字符一共出现几次

1 #统计指定字符的个数

2 stc_1 = "We are practice"

3 stc_2 = stc_1.count("e")4 print(stc_2)5 #执行结果

6

7 #可以查找自己想要搜索的范围

8 stc_3 = stc_1.count("e",5,15)9 #执行结果

10 print(stc_3)

- (startswith)(endswith)检查指定的首,尾字母,返回布尔值

1 #检查指定首,尾字母,返回布尔值

2 #使用(startswitch)检查指定首字母

3 sts_1 = "We are practice"

4 print(sts_1.startswith("w"))5 #执行结果

6 False7 print(sts_1.startswith("W"))8 #执行结果

9 True10

11 #检查指定结尾字母,返回布尔值

12 print(sts_1.endswith("s"))13 #执行结果

14 False15 print(sts_1.endswith("e"))16 #执行结果

17 True

- (isalpha)检查字符串是否为字母字符串

1 #检查字符串是否为字母字符串,返回布尔值

2 st_1 = "We are practic"

3 st_2 = "Wearepractic"

4

5 print(st_1.isalpha())6 #执行结果

7 False8 print(st_2.isalpha())9 #执行结果

10 Ture

- (isalnum)检查字符串是否为数字或字母组成

1 #检查字符串是否由数字或字母组成(可以是数字+字母),返回布尔值

2 st_1 = "Good yo123"

3 st_2 = "Goodyo123"

4 st_3 = "Goodyo"

5 st_4 = "123456"

6

7 print(st_1.isalnum())8 #执行结果

9 False10 print(st_2.isalnum())11 #执行结果

12 Ture13 print(st_3.isalnum())14 #执行结果

15 Ture16 print(st_4.isalnum())17 #执行结果

18 Ture

- 检查字符串是否由数字组成,返回布尔值

- isdigit:Unicode数字(统一码),byte数字(单字节),全角数字(双字节),罗马数字

- isdecimal:Unicode数字(统一码),全角数字(双字节)

- isnumeric:Unicode数字(统一码),全角数字(双字节),罗马数字,汉字数字

1 #检查字符串是否由数字组成,返回布尔值

2 st_1 = "233"

3 print(st_1.isdigit())4 print(st_1.isdecimal())5 print(st_1.isnumeric())6 #上述3个输出结果

7 Ture8 Ture9 Ture10

11 #小数点不是数字

12 st_2 = "23.3"

13 print(st_2.isdigit())14 print(st_2.isdecimal())15 print(st_2.isnumeric())16 #上述3个输出结果

17 False18 False19 False20

21 st_3 = "五十"

22 print(st_3.isdigit())23 print(st_3.isdecimal())24 print(st_3.isnumeric())25 #上述3个输出结果

26 False27 False28 Ture29

30 #这个不是字母I是罗马数字1

31 st_4 = "Ⅰ"

32 print(st_4.isdigit())33 print(st_4.isdecimal())34 print(st_4.isnumeric())35 #上述3个输出结果

36 False37 False38 Ture

- (split)指定字符切割字符串,返回列表

1 #使用指定的字符切割字符串,返回列表

2 st = "生活不分学期-你并没有暑假可以休息-也没有哪位雇主乐于帮你发现自我-自己找时间做吧。"

3

4 print(st.split("-"))5 #执行结果

6 ['生活不分学期', '你并没有暑假可以休息', '也没有哪位雇主乐于帮你发现自我', '自己找时间做吧。']

- (splitlines)用换行符切割字符串,返回列表

1 #使用换行符切割字符串,返回列表

2 st = "生活不分学期\n你并没有暑假可以休息\n也没有哪位雇主乐于帮你发现自我\n自己找时间做吧。"

3

4 print(st.splitlines())5 #执行结果

6 ['生活不分学期', '你并没有暑假可以休息', '也没有哪位雇主乐于帮你发现自我', '自己找时间做吧。']

- (join)用指定字符串使列表拼接,返回字符串

1 #指定字符串将列表连接,返回字符串

2 st_1 = ['生活不分学期', '你并没有暑假可以休息', '也没有哪位雇主乐于帮你发现自我', '自己找时间做吧。']3 st_2 = ",".join(st_1)4

5 print(st_2)6 #执行结果

7 生活不分学期,你并没有暑假可以休息,也没有哪位雇主乐于帮你发现自我,自己找时间做吧。

- 指定字符串的长度,多余的位置用指定的字符串替换,默认空格,返回字符串

- ljust:指定字符串的长度,内容靠左边,多余的位置用指定的字符替换,默认空格,返回字符串

- center:指定字符串的长度,内容靠中心,多余的位置用指定的字符替换,默认空格,返回字符串

- rjust:指定字符串的长度,内容靠右边,多余的位置用指定的字符替换,默认空格,返回字符串

1 st = "Hell"

2

3 print(st.ljust(6)+ "h")4 #执行结果

5 Helloo6

7 print(st.center(6,"o"))8 #执行结果

9 oHello10

11 print(st.rjust(6,"o"))12 #执行结果

13 ooHell

- 删除指定的字符,默认是空格

- strip:删除左右两边指定的字符,默认是空格

- lstrip:删除左边指定的字符,默认是空格

- rstrip:删除右边指定的字符,默认是空

1 stt = "aabbbcc"

2 print("##"+ stt.strip() + "##")3 #执行结果

4 ##aabbbcc##

5 st = "aaabbbaaa"

6

7 print(st.strip("a"))8 #执行结果

9 bbb10

11 print(st.lstrip("a"))12 #执行结果

13 aaabbb14

15 print(st.rstrip("a"))16 #执行结果

17 bbbaaa

- (zfill)指定字符串的长度,内容靠右,多余的用0填充,返回字符串

1 #(zfill)指定字符串的内容长度,内容靠右,返回字符串

2 st = "Hello"

3

4 print(st.zfill(10))5 #执行结果

6 00000Hello

- (maketrans)指定字符串替换映射表

1 #(maketrans)生产指定字符串替换的生成表,返回编码

2 st = "感觉自己萌萌哒"

3 st_2 = st.maketrans("萌萌哒","虎虎的")4

5 print(st_2)6 #执行结果

7 {33804: 34382, 21714: 30340}8

9 #(translate)转化为字符串,返回字符串

10 print(st.translate(st_2))11 #执行结果

12 感觉自己虎虎的

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值