函数 | 功能描述 |
name.capitalize() | 首字母大写 |
name.count('x') | 查找某字符x在字符串内出现的次数 |
name.find('x') | 查找字符x在字符串内第一次出现的位置,返回其下标,如果不存在返回-1 |
name.index('x') | 查找字符x在字符串内第一次出现的位置,返回其下标,如果不存在报错 |
name.replace(olderstr, newstr) | 查找替换,以newstr替换oldstr |
函数 | 功能 |
name.strip() | 去掉字符串开头和结尾的空格和换行符 |
name.strip('x') | 去掉开头和结尾指定字符或字符串 |
name.lstrip('x') | 去掉字符串左边的空格和换行符或指定的字符x |
name.rstrip('x') | 去掉字符串右边的空格和换行符或指定的字符x |
函数 | 功能 | |
name.isalnum() | 判断字符串是否全部为字母或数字,并且不为空 | |
name.isalpha() | 判断字符串是否全部为字母,并且不为空 | |
name.isdigit() | 判断字符串是否全部为数字,并且不为空 | |
name.isspace() | 判断字符串是否全部为空白字符, | |
name.islower() | 判断字符串是否全部为小写字母 | |
name.isupper() | 判断字符串是否全部为大写字母 | |
name.istitle() | 判断字符串是否全部首字母大写 |
A = 'this is a good day'
print(A.split(" "))
结果:
['this', 'is', 'a', 'good', 'day']
A = ['this', 'is', 'a', 'good', 'day']
print(' '.join(A))
结果:
this is a good day
A = 'this'
print(' is'.join(A))
结果:
t ish isi iss
会在序列的两个元素之间插入连接的字符。
+:字符串拼接
*:字符串重叠追加
print("*2"*3)
print("hello"+"world")
结果:
*2*2*2
helloworld
从0开始
print("hello"[1])
结果:
e
s[n:m:i]
n是开启的索引,m是结束的索引但是不包括该位置,i是步长。
正向下标从0开始到len(s)-1
反向下标从负的len(s)到-1
s = "hello world"
s1 = s[0:5:2]
s2 =s[0:]
s3 = s[-4:-1]
print(s)
print(s1)
print(s2)
print(s3)
s = "hello" "world"
print(s)
结果
helloworld
str():面向用户和终端
repr():面向机器
s = "hello world"
print(repr(s))
print(str(s))
结果:
'hello world'
hello world
s = ' dsds dsd '
s1= s.encode()
print(type(s1))
rjust()该方法返回字符串的右边的长度宽度,填充是通过使用指定的fillchar(默认为空格)。如果宽度小于len(s)返回原始字符串。
rjust(width[, fillchar])
s = "hello"
print(s.ljust(10,'0'))
#!/usr/bin/python3
intab = "aeiou"
outtab = "12345"
trantab = str.maketrans(intab, outtab)# 制作翻译表
str = "this is string example....wow!!!"
print(str.translate(trantab))
结果:
th3s 3s str3ng 2x1mpl2....w4w!!!
bytes 只负责以字节序列的形式(二进制形式)来存储数据,至于这些数据到底表示什么内容(字符串、数字、图片、音频等),完全由程序的解析方式决定。
bytes 类型的数据非常适合在互联网上传输,可以用于网络通信编程;
1)bytes构造函数
bytes(iterable_of_ints):参数iterable_of_ints代表整数可迭代对象,包括元组,列表等,其整数不能超过255。
2)字节操作符运算
3)操作函数
bytes类型和字符串类型的操作函数几乎一样。而bytearray类型要比bytes类型多一些增删方面的函数。
bytearray是可变的字节数组
remve(value)删除列表中某个值的第一个匹配项;
pop(index =-1) 移除字节序列列表中的一个元素,默认最后一个元素。
insert(index,item)在下标为index的位置插入新的元素item。原下标位置的元素向后推移。
append(int) 尾部追加一个元素
extend(iterable_of_ints)将一个可迭代的整数结婚追加到当前bytearray
decode()方式
bytes()方式
s = b' dsds dsd '
s1= s.decode()
print(type(s1))
#为 bytes() 方法指定字符集
b4 = bytes('Python31岁了', encoding='UTF-8')#UTF-8中文占3个字节
print("b4: ", b4)
结果
b4: b'Python31å²äº'
注意:中文乱码的原因,编解码采用包不同的编码格式时。
Little Endian 和Big Endian是计算机字节顺序的两种格式,Little Endian 把低字节存放在内存的低位;Big Endian把低字节存放在内存高位。
如果将0x1234abcd写入以0x0000开始的内存中,则
传输时如果发送端和接收端计算机的字节顺序不一致需要进行转换。
在编码格式章节有详细说明。
print('
'.join(
[' '.join
(
[
('Love'
[
(x - y) % len('Love')
]
if ((x * 0.05) **2 + (y * 0.1) **2 - 1) **3 - (x * 0.05) **2 * (y * 0.1) **3 <= 0 else ' '
)
for x in range(- 30, 30)
]
) for y in range(30, -30, -1)
]
)
)
若有收获,就点个赞吧
留言与评论(共有 0 条评论) “” |