Python学习第三天(持续学习了很多的str类型相关方法)

今天的主要内容是各种各样的str对应方法,就直接把自己测试的东西放在了下面:
还有很多习题,这个倒是得抓紧啊。

#expandtabs:以制表符 对字符串进行断句,并根据参数数字补齐位数

test = "小明 18 xiaoyao@nfh.hk 小花hua 20 786492437@nfh.hhk 小明 18 xiaoyao@nfh.hk 小花hua 20 786492437@nfh.hhk"
v = test.expandtabs(20)
print(v)

#test.isalnum() 判定字符串test是否全部为数字
#test.isalpha() 判定字符串test是否全部为字母、汉字

#str.isdecimal() 判断字符串是否全部为十进制数字 2
#str.isdigit() 判断字符串是否全部为十进制数字 包含特殊情况的 ②
#str.isnumeric() 以上两种都能识别,同时可判断是否为汉字数字 二

#str.isprintable() 判断字符串中是否有不可显示部分,如包含 、 这些不能直接显示的则为false
#str.isspace() 判断是否全部为空格
#str.istitle() 判断是否为英文题目(首字母都是大写)

#test = "i am xibei kongtong daxia"
#v = test.title()
#print(v)

#非常重要的join:拆分字符串并加入制定字符

#test = "你是风儿我是沙"
#a = "~~"
#v = a.join(test)
#print(v)

#关于ljust和rjust
#test = "Alex"
#v1 = test.ljust(20,"~") #输出20个“~”,并将制定字符串放在他的左边
#v2 = test.rjust(20,"x") #输出20个“~”,并将制定字符串放在他的右边
#print(v1," ",v2)

#str.islower() 判断是否全部为小写
#str.lower() 将其全部转变为小写

#str.isupper() 判断是否全部为大写
#str.upper() 将其全部转变为大写

#strip()去除字符串中空白或“ ”指定内容,如果没有完全匹配,则优先去除最长共子集
# 移除指定字符串

# 有限最多匹配优先进行去除
# test = "xa"
# # v = test.lstrip('xa')
# v = test.rstrip('9lexxexa')
# # v = test.strip('xa')
# print(v)
# 去除左右空白 # 去除

#maketrans和translate maketrans确定两个长度相同字符串的对应关系
#translate按照maketrans确定的对应关系进行转换
test = "aeiou"
test1 = "12345"
v = "asidufkasd;fiuadkf;adfkjalsdjf"
m = str.maketrans("aeiou", "12345")
new_v = v.translate(m)
print(new_v)

#运算结果:new_v = "1s3d5fk1sd;f351dkf;1dfkj1lsdjf"

# 分割为三部分(找到指定字符就不在继续分割)
# test = "testasdsddfg"
# v = test.partition('s')
# print(v)
# v = test.rpartition('s')
# print(v)

# 22 分割为指定个数(根据所录入参数)
# v = test.split('s',2)
# print(v)
# test.rsplit()


# 23 分割,只能根据,true,false:是否保留换行
test = "asdfadfasdf asdfasdf adfasdf"
v = test.splitlines(False)
print(v)

# 24 以xxx开头,以xx结尾
# test = "backend 1.1.1.1"
# v = test.startswith('a')
# print(v)
# test.endswith('a)

# 25 大小写转换(大换小,小换大)
# test = "aLex"
# v = test.swapcase()
# print(v)

# 26 字母,数字,下划线 : 标识符 def class
# a = "def"
# v = a.isidentifier()
# print(v)


# 27 将指定字符串替换为指定字符串
# test = "alexalexalex"
# v = test.replace("ex",'bbb')
# print(v)
# v = test.replace("ex",'bbb',2)
# print(v)



###################### 7个基本方法 ######################
# join # '_'.join("asdfasdf")
# split
# find
# strip
# upper
# lower
# replace
###################### 4个特别的方法 ######################
# test = "郑建文妹子有种冲我来"

# 一、for循环
# for 变量名 in 字符串:
# 变量名
# break
# continue


# index = 0
# while index < len(test):
# v = test[index]
# print(v)
#
# index += 1
# print('=======')

# for zjw in test:
# print(zjw)

# test = "你是风儿我是沙,去你妹的"
# for item in test:
# print(item)
# break

# for item in test:
# continue
# print(item)

# 二、索引,下标,获取字符串中的某一个字符
# v = test[3]
# print(v)

# 三、切片
test = "nisshia"
v = test[0:-1] # 0=< <1 0到-1就是全部的意思
print(v)

# 四、获取长度
# Python3: len获取当前字符串中由几个字符组成
# v = len(test)
# print(v)

就是这些了


原文地址:https://www.cnblogs.com/xiaoyaotx/p/12364496.html