博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
day10,11-Python 基本数据类型介绍之数字与字符串(看看就好)
阅读量:6594 次
发布时间:2019-06-24

本文共 3369 字,大约阅读时间需要 11 分钟。

数字:int  

#字符串转换整型a = "123"print(type(a),a)b = int(a)print(type(b),b)b = b + 1000print(b)

字符串:str

首字母大写:capitalize()

test = "charon"v =test.capitalize()print(v)

所有变小写lower()

test = "chAron"v1 = test.lower()print(v1)

设置宽度,并将内容居中,20代表总长度。*代表空白位置填充,一个字符可有可无

test = "charon"v2 = test.center(20,"*")print(v2)

去字符串中寻找,寻找子序列出现的次数。可以定义起始位置 跟终止位置 

test = "charoncccc"v3 = test.count("c")print(v3)

以什么开头或者结尾startswith(),endswith()

test = "charon"v4 = test.startswith("ch")print(v4)#返回Truev5 = test.endswith("ch")print(v5)#返回False

 

格式化,将一个字符串中得占位符替换为指定的值format()

test = "i am {name},age {a}"v5 = test.format(name = "charon",a = 19)print(v5)

高级点:

test = "i am {0},age {1}" #占位符0,1,2,3......v5 = test.format("charon",19)print(v5)

另一种格式化,跟上面相同。利用字典格式format_map()

test = "i am {name},age {a}"v5 = test.format(name = "charon",a = 19)v6 = test.format_map({"name":"pluto","a":20})print(v5)print(v6)

判断字符串中是否只含有数字和字母isalnum()

test = "charon123456"v7 = test.isalnum()print(v7)#如果真返回True,假如含有符号便返回False

判断字符串是否只含字母islpha()

test = "charon123456"v8 = test.isalpha()print(v8)#真返回True,假返回False

判断字符串是否为数字 isdigit()

test = "123456"v9 = test.isdigit()print(v9)#真返回True,假返回False

判断是否是标题istitle(),title()

test = "Return True if all cased characters in S are uppercase and there is"#判断是否为标题v10 = test.istitle()print(v10)#转化为标题v11 = test.title()print(v11)#再次判断是否为标题v12 = v11.istitle()print(v12)结果:/ecapp/python3.6/bin/python3.6 /opt/day10/s1.pyFalseReturn True If All Cased Characters In S Are Uppercase And There IsTrueProcess finished with exit code 0

将字符串的每一个元素按照指定分隔符进行拼接

test = "iamcharom"v13 = "-".join(test)print(v13)结果:i-a-m-c-h-a-r-o-m

设置宽度,并将内容居最左,右。20代表总长度,*代表空白位置填充ljust(),rjust()

test = "charom"v14 = test.ljust(20,"*")v15 = test.rjust(20,"*")print(v14)print(v15)结果:charom****************************charom

判断是否全部为小写,大写和转换(有点像验证码)

test = "Charom"v16 = test.islower()#判断是否为小写v17 = test.lower()#转换为小写print(v16,v17)v18 = v17.islower()#再次判断是否为小写print(v18)v19 = test.isupper()#判断是否为大写v20 = test.upper()#转换成大写print(v19,v20)v21 = v20.isupper()#再次判断是否为大写print(v21)结果:False charomTrueFalse CHAROMTrue

 默认去除左右空白、\n、\t

test = "   charom\t"v22 = test.lstrip()print(v22)v23 = test.rstrip()print(v23)v24 = test.strip()print(v24)结果:charom	   charomcharom

分割

test = "charonchaoronaxzc"v25 = test.partition("a")v26 = test.rpartition("a")v27 = test.split("a")v28 = test.split("a",1)v29 = test.rsplit("a",1)print(v25)print(v26)print(v27)print(v28)print(v29)结果:('ch', 'a', 'ronchaoronaxzc')('charonchaoron', 'a', 'xzc')['ch', 'ronch', 'oron', 'xzc']['ch', 'ronchaoronaxzc']['charonchaoron', 'xzc']

替换

test = "charoncharoncharon"v30 =  test.replace("ch","bbb")v31 = test.replace("ch","bbb",1)v32 = test.replace("ch","bbb",2)print(v30)print(v31)print(v32)结果:bbbaronbbbaronbbbaronbbbaroncharoncharonbbbaronbbbaroncharon

六个基本会的

join,split,strip,upper,lower,replace

**********************************************************

索引,下标,获取字符串中得某一个字符

test = "charon"v = test[2]v1 = test[0:2]#>=0 <2(下标)v2 = test[0:-1]v3 = len(test)print(v)print(v1)print(v2)print(v3)结果:achcharo6
test = "charon"index = 0while index < len(test):    print(test[index])    index += 1print("=======")

range    range(1,100,5)

v = range(100)for item in v:    print(item)

 

test = input(">>>:")for item in range(0,len(test)):    print(item,test[item])

 

  

 

四个基本会的  

for。len。索引。切片,range

 深灰魔法

字符串一旦创建就不可修改

转载于:https://www.cnblogs.com/charon2/p/10333143.html

你可能感兴趣的文章
原型继承
查看>>
找不到ifconfig命令
查看>>
微服务事务处理
查看>>
用Groovy进行单元测试
查看>>
github地址
查看>>
nginx使用
查看>>
两个openssh间免密码登录
查看>>
【linux】 linux gpio操作
查看>>
【linux kernel】 softirq 软中断讨论
查看>>
2019武汉大学数学专业考研真题(回忆版)
查看>>
百度地图车辆运动轨迹
查看>>
RE模块错误已解决.
查看>>
文本与字体
查看>>
汕头市队赛 yyl杯1 T1
查看>>
从函数式编程到Ramda函数库(一)
查看>>
ora-1652
查看>>
PL/SQL developer 开发小技能 and ash show command PL/SQL EXECUTE
查看>>
Linux oraenv Tips
查看>>
27-列表解析
查看>>
Java并发--线程安全策略
查看>>