python之路第五天

字符串的应用(二)

expandtabs 断句16,不够16个,用空格补齐

s = "username\te-mail\tpassword\nxiaoming\t123@qq.com\t123456"
v = s.expandtabs(16)
print(v)

isalpha判断是否是字母,汉字

test = "asdf"
v = test.isalpha()
print(v)

test = "as2df"
v = test.isalpha()
print(v)

 

 当前输入的是否是数字

isdecimal:只支持单纯的数字

isdigit:不仅支持数字而且还支持特殊符号的数字

isnumeric不仅支持数字而且还支持特殊符号的数字,还支持中文的数字
test = ""
v1 = test.isdecimal()
v2 = test.isdigit()
v3 = test.isnumeric()
print(v1,v2,v3)

 

 

 

test = ""
v1 = test.isdecimal()
v2 = test.isdigit()
v3 = test.isnumeric()
print(v1,v2,v3)

 

 

 

test = "123"
v1 = test.isdecimal()
v2 = test.isdigit()
print(v1,v2)

 

 

test = "aa123"
v1 = test.isdecimal()
v2 = test.isdigit()
print(v1,v2)

 

 

 

 字母  数字  下划线  :标识符

判断该变量是否满足标识符规则

a = "_123"
v = a.isidentifier()
print(v)

a = "123"
v = a.isidentifier()
print(v)

 

 isprintable:是否存在不可显示的字符

\t 制表符

\n 换行

test = "oiua\tsdfj"
v = test.isprintable()
print(v)

test = "oiuasdfj"
v = test.isprintable()
print(v)

 

 判断字符串里是否全部都是空格

test = "    "
v = test.isspace()
print(v)

 

test = "a b c    d"
v = test.isspace()
print(v)

istitle:判断是否是标题

title:将字符转换为标题

何为标题?  字符串 中的每个字符首字母是大写

 

 

test = "Hao  hao  xue  xi  Tian  tian xiang  shang "
v1 = test.istitle()
v2 = test.title()
v3 = v2.istitle()
print(v1)
print(v2)
print(v3)

 

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

test = "你是风儿我是沙"
print(test)
t = ' '
v = t.join(test)#  相同   v = ' '.join(test)
print(v)

填充字符串

center:居中填充

ljust:左填充

rjust:右填充

zfill:默认填充的是0

test = "xiaoming"
v1 = test.center(20,"*")
v2 = test.ljust(20,"*")
v3 = test.rjust(20,"*")
v4 = test.zfill(20)
print(v1)
print(v2)
print(v3)
print(v4)

 

 字符串大小写的判断与转换

test = "Xiaoming"
v1 = test.islower()   #判断是否全部是小写
v2 = test.lower()     #转换为小写
print(v1, v2)

 

v1 = test.isupper()     #判断是否全部是大写
v2 = test.upper()       #转换为大写
print(v1, v2)

 

 去除左右空格

test = "  xiaoming  "
v1 = test.lstrip()
v2 = test.rstrip()
v3 = test.strip()
print(v1)
print(v2)
print(v3)

去除\n    \t 

test = "\nxiaoming  "
v1 = test.lstrip()
v2 = test.rstrip()
v3 = test.strip()
print(v1)
print(v2)
print(v3)

test = "\txiaoming  "
v1 = test.lstrip()
v2 = test.rstrip()
v3 = test.strip()
print(test)
print(v1)
print(v2)
print(v3)

 

 移除指定字符

test = "xiaoming"
v1 = test.lstrip('xi')#从左边移除指定的字符
v2 = test.rstrip('ng')#从右边移除指定的字符
v3 = test.strip('ng')#移除的字符必须是原字符串首尾且连续的子字符,如果没有首或尾的子字符,将不能移除

print(v1)
print(v2)
print(v3)

 替换文字

v = "你是谁,风好大,我听不清"
m = str.maketrans("你是风儿我是沙", "缠缠绵绵到天涯")
new_v = v.translate(m)
print(new_v)

 

 

下面开始讲解 灰 魔法  了解一下

#索引 , 下标,获取字符串中的某个自字符
test = "alex"
# v = test[0]
# print(v)

#切片
# v = test[0:-1]  #0:1  0=<   <1
# print(v)

#len  获取当前字符串中有几个字符组成

# v = len(test)
# print(v)
li = [11,22,33,44,55,"asdf"]
v1 = len("asdfgh")
v2 = len(li)#  它计算的是用逗号隔开的分组元素
print(v1, v2)

test = "好好学习天天向上"
# index = 0
# while index < len(test):
#     v = test[index]
#     print(v)
#     index += 1
# print('===============')
#for  循环
# for  变量名 in 字符串:
#     print(变量名)
for xm in test:
    print(xm)

 字符串相加

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

一旦修改或者拼接,都会重新生成新的字符串

name = "xiaoming"
age = "18"
info = name + age
print(info)

原理及形成

 

替换

 

test = "alexalexalexalex"
# v = test.replace("ex", 'bbb')
# print(v)

 

 

 

 

test = "alexalexalexalex"
v = test.replace("ex", 'bbb',2) # 2是从左到右替换的次数
print(v)

 

 

range 的基础用法

帮助创建连续的数字

输出0-100

v = range(100)#  (0,100,5)
for item in v:
    print(item)

 

 

练习《《《《《《《《《《

 

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

 

posted @ 2018-07-19 17:31  贾江浦  阅读(253)  评论(0编辑  收藏  举报