码迷,mamicode.com
首页 > 编程语言 > 详细

python字符串操作

时间:2017-12-28 13:53:25      阅读:131      评论:0      收藏:0      [点我收藏+]

标签:规则   找不到   tabs   make   isa   exp   pca   wap   format   

 1 #!/usr/bin/env python
 2 # -*- coding: utf-8 -*-
 3 # Auther: Summer
 4 
 5 # 字符串操作
 6 name = "summer Du"
 7 print(name.capitalize())  # 首字母变大写
 8 print(name.casefold())  # 大写字母全部变小写
 9 print(name.center(50,"="))  # 输出====================summer Du=====================
10 print(name.count("m"))  # 统计m出现次数
11 print(name.encode())  # 将字符串编码成bytes格式
12 print(name.endswith("m"))  # 判断字符串是否以m结尾
13 print(name.find("D"))  # 查找D,找到返回其索引值,找不到返回-1,返回其找到第一的索引值
14 
15 name = "sum\tmer Du"
16 print(name.expandtabs(10))  # 输出sum       mer Du
17 
18 # format
19 msg = "my name is {},and age is {}"
20 print(msg.format("summer","24"))
21 msg = "my name is {1},and age is {0}"
22 print(msg.format("summer","22"))
23 msg = "my name is {name},and age is {age}"
24 print(msg.format(name = "summer",age = "22"))
25 print(msg.format(age = "22",name = "summer"))
26 
27 # format_map
28 print(msg.format_map({age: 22,name: "summer"}))
29 msg = "my name is {name},and age is {age}"
30 print(msg.index("m"))
31 
32 print("9Aa".isalnum())
33 print("9".isdigit())   # 是否整数
34 print(name.isnumeric())
35 print(name.isprintable())
36 print(name.isspace())
37 print(name.istitle())
38 print(name.isupper())
39 name = "summer Du"
40 print(name.join("lee"))
41 print("|".join(["lee","summer"]))  # 放到列表里
42 
43 # maketrans  Python maketrans() 方法用于创建字符映射的转换表,
44 # 对于接受两个参数的最简单的调用方式,第一个参数是字符串,表示需要转换的字符,
45 # 第二个参数也是字符串表示转换的目标。
46 intab = "aeiou"
47 outtab = "12345"
48 trantab =str.maketrans(intab,outtab)
49 print(trantab)
50 
51 # partition
52 print(msg.partition("is"))
53 
54 r = "my name is summer,and age is 22"
55 print(r.replace("m","n",2))
56 
57 print(name.swapcase())   # 大小写互换
58 
59 print(msg.zfill(40))
60 print(msg.ljust(40,"-"))
61 print(msg.rjust(50,"="))
62 print(msg.center(50,"="))
63 
64 b = " ddefs;ajfdal;_hahO(∩_∩)O哈哈~"
65 c = "dd_哈哈"
66 print(b.isidentifier())  ##检测一段字符串可否被当作标志符,即是否符合变量命名规则
67 print(c.isidentifier())  ##检测一段字符串可否被当作标志符,即是否符合变量命名规则

 

python字符串操作

标签:规则   找不到   tabs   make   isa   exp   pca   wap   format   

原文地址:https://www.cnblogs.com/summerxye/p/8134942.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!