python字符串列表内部函数

字符串
字符串是不可变的
作用是描述:记录描述事物性质状态
定义:单引号双引号三引号之间
特点:字符串中的字符(注无字符概念只为了描述),在内存中是按照索引有序存放的,因此可以通过索引取值

内部函数:

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]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值