D11-04字符串(六)

test.isdecimal()
test.isdigit()
test.isnumeric()
isdecimal() 
True: Unicode数字,全角数字(双字节)
False: 罗马数字,汉字数字,小数
Error: byte数字(单字节)
如果字符串中只有十进制字符,则返回True,否则为False。
isdigit()
True: Unicode数字,byte数字(单字节),全角数字(双字节)
False: 汉字数字,罗马数字,小数
Error: 无
如果字符串中的所有字符都是数字,并且在S中至少有一个字符,则返回True。
isnumeric() 
True: Unicode数字,全角数字(双字节),罗马数字,汉字数字
False: 小数
Error: byte数字(单字节)
翻译:如果字符串中只有数字字符(各种数字3三叁Ⅲ),则返回True,否则为False。

isprintable 是判断字符串是否可打印 如果字符串中包含\t \n 等打印后不可见的 制表和换行等符号 都判断为flase
isspace 判断是否全部为空格 必须全部是空格才是true
istitle 判断是否为标题 每个字母首字母是大写才能为true



join:
test = '你是风儿我是啥'
print(test)
t = '*'
v = t.join(test)    也可写作 v = '&'.join(test) 输出结果为  你&是&风&儿&我&是&啥 .join前面是任何字符串都可以
print(v)
输出结果

    你是风儿我是啥
    你*是*风*儿*我*是*啥

 将t加入test 按照字面意思理解 将字符串中的每一个原色按照指定分隔符进行拼接插入


l just:
test = 'liu'
v = test.ljust(20,'*')
print(v)
数据结果 liu*****************

   l just 原字符放在左边右边填充 r just 字符放右面左边填充

==========

lstrip()  和 rstrip() 和strip() 函数主要是去除左右空白格 
lstrip() 去除左边空格 rstrip()去除右边空格 strip()左右两侧都去
如果遇到换行符 \n 和制表符\t 也都是可以去除的
另外也可以去掉指定字符 先进行子序列的最多匹配
test = 'liupingtao'
v = test.strip('iultagopn')

print(v)
输出结果为空 #因为iultagopn 包含liupingtao中所有的元素 去除后打印为空

=====

  maketrans() 和translate() 对应看

test = 'aeiou'    
test1 = '12345'
v = 'aeioddu12345dlfaf95'  
m = str.maketrans('aeiou','12345') #建立对应规则 用make
v.translate(m)   #对规则进行翻译用trans
print(v)

输出结果为aeioddu12345dlfaf95

======

分割

test.partition()默认左边开始分割
test.rpartition() 从右分割

test = 'testdfaljfld'
v = test.partition('s') #遇到s进行分割  #无法制定第几个s永远被分割成三份
输出结果('te', 's', 'tdfaljfld')

 test.split()

test.rsplit()
test = 'testdftaljyltd'
v = test.split('t')
print(v)
输出结果为['', 'es', 'df', 'aljyl', 'd'] #按照t分割 但是不取t

 

test = 'testdftaljyltd'
v = test.split('t',2)
print(v)
输出结果为['', 'es', 'dftaljyltd'] #传参是2 可以分割成三段 即 n+1段
splitlines()  #换行
test = '123ldf\nadfped\nlajfnve'
v = test.splitlines()
print(v)
输出结果为 ['123ldf', 'adfped', 'lajfnve']

 

splitlines() 只能根据换行符分割 True 和False 决定是否保留换行符
test = '123ldf\nadfped\nlajfnve'
v = test.splitlines(True)
print(v)
输出结果为['123ldf\n', 'adfped\n', 'lajfnve'] #括号内为True 则保留换行符,False去掉换行符

=====

startswith()和endswith()   #以 xx 开始 和以 xx结束

test = '123124hdakfa'
v = test.startswith('123')
print(v)
输出结果为True

=====

swapcase() #大小写转换
test = 'Aiu'
v = test.swapcase()
print(v)
输出结果为aIU 

======

zfill()  以0 补齐

 ======

test = 'liupingtaoliupingtao'
v = test.replace('iu','dafdff')
print(v)
输出结果 ldafdffpingtaoldafdffpingtao  #执行替换操作 默认替换全部

  

test = 'liupingtaoliupingtao'
v = test.replace('iu','dafdff',1)
print(v) 
输出结果ldafdffpingtaoliupingtao #替换一个

  

********划重点*******
必会magic
join、split、find、strip、upper、lower、replace
test = 'liupingtao'
v = test[3]
print(v)
输出结果为  p

 根据索引(也叫作下标)获取字符串中的某一个

test = 'liupingtao'
v = test[0:3]
print(v)
输出结果为liu  #切片工具根据左闭右开的原则

len() 获取字符串中有几个字符组成

test = 'liupingtao'
v = len(test)
print(v)
输出结果 10

 

利用索引和循环 取字   

test = '哇哈哈哈,高薪快来'
count = 0
while count <len(test):
    v = test[count]
    print(v)
    count += 1
输出结果









 

for 循环在其他的操作里

for i in test:
print(i) #该效果同 while 循环 超级简单的循环

=================

字符串一旦创建就不可修改,一旦修改或者拼接都会重新生成字符串。-----原理

 ========range 可以创建连续的数字 即 for i in range(0,100) 

     可以加步长 创建不连续的数字 for i in range(0,100,4) 4即为步长

test = input('随便输入内容:')
print(test)
l = len(test)
r = range(0,l)
for i in r:
    print(i,test[0])

输出结果为
随便输入内容:ddafdf
ddafdf
0 d
1 d
2 d
3 d
4 d
5 d

  以上代码可简写:

test = input('随便输入内容:')
print(test)
for i in range(0,len(test)):
    print(i,test[i])
输出结果为
随便输入内容:dfdfdfsajj
dfdfdfsajj
0 d
1 f
2 d
3 f
4 d
5 f
6 s
7 a
8 j
9 j

  

posted @ 2018-06-13 15:51  犀利的攻城狮  阅读(149)  评论(0)    收藏  举报