标签:包含 rip strip 首字母 lines hello 设置 expand format
Python中包括以下常用数据类型:数字、布尔值、 字符串、列表、元组、字典、集合、collention系列
关键字:int
a = "123" print(int(a)) #转换成数字 b = 5 print(b.bit_length()) #返回表示数字时占用的最少位数
关键字:bool
表示真或假,True或False
为False的表示:0、””(空字符串)、[](空列表)、()(空元组)、{}(空字典)
为True的表示:非零的数字、非空的其他数据类型对象
关键字:str
字符串常用功能:
1、替换操作:
test18 = "alexalexalex"
print(test18.replace("ex", "bbb"))     #把字符串中指定的字串替换,默认把全部的替换
print(test18.replace("ex", "bbb", 2))  #把字符串中指定的字串替换,第三个参数指定替换其中的前几个
2、查找操作
str1 = "    hello world "
print(str1.find("o"))        #在字符串中查找指定的字串,返回其下标,未找到返回-1
print(str1.index("o"))       #在字符串中查找指定的字串,返回其下标,未找到返回错误
3、字符串格式化
test1 = "i am {name}, age {a}"
print(test1.format(name="baihl", a=10))   #格式化输出
test2 = "i am {0}, age {1}"
print(test2.format("baihl", 10))         #格式化输出
print(test1.format_map({‘name‘:"baihl", ‘a‘:10}))   #使用字典方式格式化
4、分割操作
test10 = "心梦无痕"
str1 = "_"
print(str1.join(test10))  #表示以字符串_分割字符
print(" ".join(test10))   #以空格分割
test15 = "absdfsnlkjsljlkj"
print(test15.partition("s"))   #指定以s字符从左开始分割字符串,只分割一次
print(test15.rpartition("s"))  #指定以s字符从右开始分割字符串,只分割一次
print(test15.split("s", 2))    #指定以s字符从左开始分割字符串,分割2次,如果没有第二个参数,默认全部分割,分割后s不包含在分割后的字符中
print(test15.rsplit("s", 2))
test16 = "ajf\nadfjadfjadf\nfajdfjj"
print(test16.splitlines())      #默认使用\n进行字符串分割,分割后的字符串中不包含\n
print(test16.splitlines(True))  #默认使用\n进行字符串分割,分割后的字符串中包含\n
print(test16.splitlines(False)) #同不带参数
5、删除空格操作:
test12 = " \nalex "
print(test12.lstrip())    #删除左边的空格或左边的换行
print(test12.rstrip())    #删除右边的空格。。。
print(test12.strip())     #删除所有的空格。。。
print("baihl".lstrip("bax"))  #从左开始匹配,把匹配到的最长字符删除,一次类推,输出"ihl"
6、开始和结束判断操作
print(strname.endswith("I"))  #表示字符串以什么结尾,输出bool值
print(strname.startswith("B")) #表示字符串以什么开始
7、大小写转换
test11 = "Alex" print(test11.islower()) #判断是否全是小写 print(test11.lower()) #全部转换成小写 print(test11.isupper()) #判断是否全是大写 print(test11.upper()) #全部转换成大写 print(test11.swapcase()) #大小写转换,把字符串中的大写转成小写,小写转成大写
8、判断字母、数字、空格、下划线:
test3 = "baihl1992" print(test3.isalnum()) #判断字符串中只包含数字或字符 test5 = "Alex中2" print(test5.isalpha()) #判断字符串是否全是字符,中文也算字符 test6 = "123" print(test6.isdecimal(), test6.isdigit()) #判断是否是数字 print(test6.isnumeric()) #判断是否是数字,可以判断中文的数字 test7 = "_\n" print(test7.isidentifier()) #判断是否是字母、数字或下划线 print(test7.isprintable()) #是否包含不可见字符 test8 = " " print(test8.isspace()) #判断字符串是否全是空格
9、判断标题
test9 = "Baihl Up li" print(test9.istitle()) #判断是否是标题,标题表示字符串中的所有单词首字母大写 print(test9.title()) #转换成标题
10、制作表格
test4 = "hello\tworld\tbaihl" print(test4) print(test4.expandtabs(6)) #表示以tab分割,每个tab加字符串的宽度为6 test4 = "username\tmail\tpasswd\nbaihl\tbaihl@qq.com\t123456\nhaoliang\thaoliang@qq.com\t123456\n" print(test4.expandtabs(20)) #制作表格,每列为20宽度
11、其他操作
strname = "HELLO WORLD"
print(strname.casefold())   #小写转换
print(strname.lower())      #小写转换
print(strname.center(20, "*"))   #字符居中,宽度20,第二个参数可选
print(strname.ljust(20,"*"))    #左对齐
print(strname.rjust(20,"*"))    #右对齐
print(strname.zfill(20))        #右对齐,使用0填充
print(strname.count("I"))   #子字符串的个数
print(strname.count("I",5)) #从第5个下标位置开始查找
print(strname.count("I",5,6))#设置查找的起始位置和结束位置
标签:包含 rip strip 首字母 lines hello 设置 expand format
原文地址:https://www.cnblogs.com/baihl/p/9063665.html