字符串:
字符串是不可变的
作用是描述:记录描述事物性质状态
定义:单引号双引号三引号之间
特点:字符串中的字符(注无字符概念只为了描述),在内存中是按照索引有序存放的,因此可以通过索引取值
内部函数:
1.upper():将所有的字符串全部变大写
2.lower():将所有的字符串变为小写。
str1="hello world"
print(str1)
print("str1.upper() %s",str1.upper())
print("str1.lower() %s",str1.lower())
hello world
str1.upper() %s HELLO WORLD
str1.lower() %s hello world
3字符串存在切片操作,尤为重要的切片操作:
[开始位置:结束位置:步长]
字符串反转
str1="hello world"
print(str1[::-1])
dlrow olleh
print(str1[-1::-1])
# 反转。
dlrow olleh
print(str1[0:6:1])
注:切片顾头不顾尾[0:6:1]索引0-5的所有串,开始位置,结束位置均为索引值,-1表示末尾。
**4.isdigit:**
判断字符串是否是纯数字,带小数点的判断为false
str2="12.3"
str3="123"
print(str2.isdigit())
print(str3.isdigit())
打印:
False
True
5.split:
用于将字符串分割为列表:
str5="hello world fuck you"
print(str5.split(" "))
['hello', 'world', 'fuck', 'you']
6.join:
用于将列表合成字符串的函数:
list1=['a','d','d']
print (":".join(list1))
a:d:d
7.replace:
str3="hhhello"
str3.replace("h","k")
kkkello
8.strip():
用于去除空格默认
str1="***hello***"
print(str1.strip("*"))
print(str1.lstrip("*"))
print(str1.rstrip("*"))
hello
hello***
***hello
9.len()
str1="***hello***"
len(str1)
11
10.find
寻找子字符串,存在返回索引值,不存在返回-1
index则是找不到报错
11.del全部删除
str1=“hhllo”
del str1
**
列表
与字符串类似存在索引取值,切片删除
1.remove删除:
word=['h','e','llo']
print(word.remove("h"))
print(word)
2.pop删除根据索引pop返回删除的元素
word=['h','e','llo']
print(word.pop())
print(word.pop(0))
print(word)
llo
h
[‘e’]
3.insert插入元素按照索引插入
word=['h','e','llo']
word.insert(0,'xxx')
print(word)
[‘xxx’, ‘h’, ‘e’, ‘llo’]
4.clear
word=['h','e','llo']
word.clear()
清空列表
5.reverse:列表倒置
word=['h','e','llo']
word.reverse()
print(word)
6.sort()同类型列表元素排序
word=['h','e','llo']
word.sort()
print(word)
[‘e’, ‘h’, ‘llo’]
7.extend()扩展
word=['h','e','llo']
word.extend([1,2,3])
print(word)
[‘h’, ‘e’, ‘llo’, 1, 2, 3]