本帖最后由 xiaolinen 于 2024-5-11 22:50 编辑
一:字符串的表达方式
1.1,使用单引号表达字符串:
a = '123'
1.2,使用双引号表达字符串:
a = "123"
1.3,使用三重双引号进行多行注释
"""
this is just an example of a comment.
"""
二:字符串的方法
2.1,索引
a = "123456"
print(a[0])
2.2,切片
a = "123456"
b = a[1:4]
print(b)
2.3,upper(),lower(),isupper()和islower()
d = c.upper()
print(d)
e = d.lower()
print(e)
print(d.isupper())
print(e.islower())
2.4,startswith()和endswith()
d = "HELLO,WORLD"
e = "hello,world"
print(d.startswith("HELLO")) # 判断字符串是不是以HELLO开头
print(e.endswith("ld")) # 判断字符串是不是以ld结尾
2.5,join()和split()
f = ["123", "456", "789"] # join()方法
g = "ABS"
g.join(f)
print(g.join(f))
h = "123Q456Q789"
print(h.split("Q")) # split()方法
2.6,partition()
i = "123qwertyhgfdsazxcv" # partition()方法
print(i.partition("y"))
2.7,rjust(),ljust()和center()
j = "^!^"
print(j.rjust(10, '*'))
print(j.ljust(10, '*'))
print(j.center(9, '*'))
2.8,strip(),rstrip()和lstrip()
k = " 123456 "
print(k.rstrip())
print(k.lstrip())
m = "qwe1234567asd765432qwe"
print(m.strip("qwe"))
2.9,ord()和chr()
n = 'N'
print(ord(n))
print(chr(77))
上述代码运行结果,如下(打印的每一行对应一个print()语句):