字符串(String)是 Python 中最常用的数据类型。我们可以使用引号‘’或“”来创建字符串。
代码实战
e = "hello,world,!!!"
f = e.split(",") #将字符串拆分成列表
print(f)
g = " ".join(f)#将列表中多个元素合并为一个字符串。并以空格隔开
print(g)
print("--------------------------")
h = " 123 "
print(h.strip())#移除字符串头尾指定的字符(默认为空格或换行符)
print("--------------------------")
i = "Hello World !!!"
print(i.lower()) #把所有的字母转化为小写字母
print(i.upper()) #把所有的字母转化为大写字母
print("--------------------------")
print("xxx999".isalnum()) #判断是字母或数字
print("xxx999&&&".isalnum())
print("999".isdecimal()) #判断是十进制数字
print("xxx999".isalpha()) #判断只是字母
print("--------------------------")
print("xxx999****".islower()) #判断有字母,且都是小写的
print("AAA999****".isupper()) #判断有字母,且都是大写的
输出结果
['hello', 'world', '!!!']
hello world !!!
--------------------------
123
--------------------------
hello world !!!
HELLO WORLD !!!
--------------------------
True
False
True
False
--------------------------
True
True
Process finished with exit code 0