标签:去除 指定 找不到 字符 pac enc 连接 replace form
定义a b c供以下使用
a = ‘hello world‘
b = ‘ ‘
c = ‘123‘
res = a.count(‘o‘)
print(res) #2
res = a.index(‘o‘)
print(res) #4
rindex 从右往左查找索引,(0,8)代表索引范围,此范围顾头不顾尾(包括0,不包括6)编号还是从左往右,且空格也算一个索引!
res = a.rindex(‘o‘,0,8)
print(res) #7
res = a.istitle()
print(res) #False
res = b.isspace()
print(res) #True
res = c.isdigit
print(res) #True
res = c.find(‘1‘)
print(res) #0
res = a.isalnum()
print(res) # False
res = a.isalpha()
print(res) #False
res = a.title()
print(res) #Hello World
res1 = a.startswith(‘h‘)
res2 = a.endswith(‘d‘)
print(res1) #True
print(res2) #True
ip = ‘192.168.133.100‘
res = ip.split(‘.‘,3)
print(res) #[‘192‘, ‘168‘, ‘133‘, ‘100‘]
name = ‘小明‘
res = name.encode(‘utf-8‘)
print(res) #b‘\xe5\xb0\x8f\xe6\x98\x8e‘
name1 = b‘\xe5\xb0\x8f\xe6\x98\x8e‘
res1 = name1.decode(‘utf-8‘)
print(res1) #小明
注:
utf-8 格式字符编码:1个中文占3个字节,生僻字会占用更多
gbk 格式的字符编码:1个中文占2个字节
用什么字符编码转码就需要用什么字符编码解开
name = ‘张三‘
age = ‘24‘
res1 = ‘my name is {},my age is {}‘.format(name,age)
res2 = ‘my name is {1},my age is {0}‘.format(age,name)
res3 = ‘my name is {a},my age is {b}‘.format(a=name,b=age)
print(res1)
print(res2)
print(res3)
name1 = ‘张三‘
age = ‘24‘
height = ‘184.567‘
res = ‘my name is %s,my age is %s‘ % (name1,age)
res1=‘my height is %.2f‘ % 184.567 #%.2f 保留两位小数 四舍五入
print(res)
print(res1) #my height is 184.57
str = "-"
seq = ("a", "b", "c")
print (str.join( seq )) #a-b-c
u = ‘====abc=====‘
res = u.strip(‘=‘)
print(res) #abc
str1=‘192.168.13.13‘
res = str1.replace(‘.‘,‘|‘,2)
print(res) #192|168|13.13
x = ‘123‘
y = ‘abc‘
print(x+y) #123abc
print(x*3) #123123123
标签:去除 指定 找不到 字符 pac enc 连接 replace form
原文地址:https://www.cnblogs.com/ylhx99/p/11715680.html