2018-7-6 Python练习

布尔判断

 

init应用

字符串(纯数字)转换为数字

a = "123"
print(type(a), a)
b = int(a)
print(type(b), b)

 

进制转换

n = "100"
v = int(n, base=16)
print(v)

当前数字二进制只要需要表示的位数

example = 5
#5-->101
r = example.bit_length()
print(r)

 

srt字符串

test = "successes GOOD"
# 首字母大写
v1 = test.capitalize()
print(v1)
# 化作小写,casefold比lower更牛逼,能转换很多未知的对应小写
v2 = test.casefold()
print(v2)
v3 = test.lower()
print(v3)

#判断字符串中是否是字符+数字
f = "da35fa$"
t = "dka3535"
f1 = f.isalnum()
t1 = t.isalnum()
print(f1, t1)

 

# 字符对其方式
test = "successes GOOD"
v1 = test.ljust(20, ">")
v2 = test.rjust(20, "<")
v3 = test.zfill(20)
print(v1)
print(v2)
print(v3)

# 字符左右间宽度30,用-替补空格
v1 = test.center(30, "-")
print(v1)

# 在test的4-9的字符中,s出现的次数
v5 = test.count("s", 4, 9)
print(v5)

 

 

表格形式

# \t表示断句,\n表示换行,配合expandtabs使用能达到表格形式的效果
test = "uname\temail\tpassword\nlingyi\tlyly@163.com\t123456abc\nlingyi2\tlyly@164.com\t123456efg\nlingyi3\tlyly@165.com\t123456hij\n"
test1 = test.expandtabs(20)
print(test1)

 

判断是否为数字

# 当前输入的是否为数字,其中isdigit能够识别特殊的数字,而isnumeric还能识别中文数字,所以结果为false false true
num = "123②二"
num1 = num.isdecimal()
num2 = num.isdigit()
num3 = num.isnumeric()
print(num1, num2, num3)

判断内容显示是否用了特殊符号显示

# 判断字符是否是可见的显示内容
v = "adada323#"
w = "fad35\t"
v1 = v.isprintable()
w1 = w.isprintable()
print(v1, w1)

# 内容是否全为空格
p = ""
q = " "
r = " f "
p1 = p.isspace()
q1 = q.isspace()
r1 = r.isspace()
print(p1, q1, r1)

 

判断是否为标题

#其中v1是false,v2是true
v = "the computer is very spuer"
v1 = v.istitle()
v2 = v.title()
v3 = v2.istitle()
print(v1, v3)

 

内容间隔很重要,需要掌握

#显示效果:我爱你 我❥(^_-)爱❥(^_-)你
v = "我爱你"
j = "❥(^_-)"
v1 = j.join(v)
print(v, v1)

 

删除个别字符(没搞懂)

#lsplit删除匹配的首字符或

test = "successes GOOD"
v1 = test.lstrip("suc")
v2 = test.rsplit("GO")
v3 = test.split()
print(v1)
print(v2)
print(v3)

 

字符转换(相当于自创密码规则)

test = "abcdefabced"
test1 = str.maketrans("abde", "1234")
new_test = test.translate(test1)
print(new_test)

 

字段分割(后面还有正则表达式的应用)

test = "abcdefabced"
test1 = test.partition("a")
print(test1)
test2 = test.rpartition("a")
print(test2)
test3 = test.rsplit("a") print(test3)

 

转载于:https://www.cnblogs.com/lylyfly/p/9272233.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值