python 字符串总结1.0

1、字符串的含义

字符串是具有特殊含义的字符组合在一起的串,是可以肉眼识别的一些符号;数据类型是弱数据类型语言;由双引号、单引号、三双引号、三单引号标注。

2、对字符串常见的操作

2.1 操作总结

操作描述
capitalize格式化字符串,将首字符转化为大写
center(width,fillchar)设置字符串长度居中,如果长度小于字符串,将不会有任何操作(width:用于控制字符串整体的长度,fillcher:决定字符串空白处填充的字符)
count统计指定的字符或者字符串在总字符串中出现的次数
encode将字符串转化为节符数据
decode将字节数据转化为字符串
endswith判断字符串是否以指定的字符或字符串结尾
startswith判断字符串是否以指定的字符或字符串结尾
find在字符串中查找第一次出现的指定的字符或字符串的位置,如果不存在,则会返回-1
rfind从右往左找,在字符串中查找第一次出现的指定的字符或字符串的位置,如果不存在,则会返回-1
index在字符串中查找第一次出现的指定的字符或字符串的位置,如果不存在,则会抛出异常
rindex从右往左找,在字符串中查找第一次出现的指定的字符或字符串的位置,如果不存在,则会抛出异常
format格式化字符串中的指定值
isalnum判断是否为有效字符
islower判断字符串是否全部为小写字母
isupper判断字符串是否全部为大写字母
istitle判断是否为标题
isdigit判断字符串是否全部为数字
isspace判断字符串是否全为空格
isalpha判断字符串是否全为字符
title将字符串转化为标题格式(也就是把每个单词首字母转换为大写)
lower将字符串中的字母转化为小写
upper将字符串中的字母转化为大写
split在指定的分隔符处拆分字符串,返回的是列表
join按照指定的格式将一个可迭代对象拼接为字符串,(注:不是容器中的方法)
strip清除字符串两侧空格
lstrip清除字符串左侧空格
rstrip清除字符串右侧空格
replace用新的字符替换旧的字符
ljust左对齐
rjust右对齐

2.2:操作展示

(1)capitalize():格式化字符串,将首字母转化为大写。
代码:

a = "hello world!"
a.capitalize()
print(a)

结果:

hello world!

(2)center(width,fillchar):设置字符串的长度并居中;width:字符串整体的长度;fillchar:代替空白部分字符串的长度。
代码:

a = "hello world!"
print(a.center(50,"*"))

结果:

*******************hello world!*******************

(3)count():用于统计指定字符在字符串中出现的次数。
代码:

a = "hello world!"
print(a.count("l"))

结果:

3

(4)encode():将字符串转化为字节数据。
    decode():将字节数据转化为字符串。
代码:

a = "hello world!"
print(type(a))
b = a.encode()
print(type(b))
c = b.decode()
print(type(c))

结果:

<class 'str'>
<class 'bytes'>
<class 'str'>

(5)endswith():判断字符串是否是以指定的字符结尾。
代码:

a = "hello world!"
print(a.endswith("!"))
print(a.endswith("b"))

结果:

True
False

(6)startswith():判断字符串是否是以指定字符开始。
代码:

a = "hello world!"
print(a.startswith("h"))
print(a.startswith("b"))

结果:

True
False

(7)find():在字符串中查找第一次出现的指定的字符或字符串的位置,如果不存在,则会返回-1
代码:

a = "hello world!"
print(a.find("w"))
print(a.find("b"))

结果:

6
-1

(8)rfind():从右往左找,在字符串中查找第一次出现的指定的字符或字符串的位置,如果不存在,则会返回-1
代码:

a = "hello world!"
print(a.rfind("o"))
print(a.rfind("b"))

结果:

7
-1

(9)index():在字符串中查找第一次出现的指定的字符或字符串的位置,如果不存在,则会抛出异常。
代码:

a = "hello world!"
print(a.index("o"))

结果:

4

(10):index():从右往左找,在字符串中查找第一次出现的指定的字符或字符串的位置,如果不存在,则会抛出异常。
代码:

a = "hello world!"
print(a.rindex("o"))

结果:

7

(11)format():格式化字符串中的指定值。
代码:

a = "hello"
b = "{} world!"
print(b.format(a))

结果:

hello world!

(12)isalnum():判断是否为有效字符。
代码:

a = "helloworld"
b = "hello world"
print(a.isalnum())
print(b.isalnum())

结果:

True
False

(13)islower():判断字符串是否全部是小写字母。
代码:

a = "hello world!"
b = "HELLO WORLD!"
print(a.islower())
print(b.islower())

结果:

True
False

(14)upper():判断字符串是否全部为大写字母。
代码:

a = "hello world!"
b = "HELLO WORLD!"
print(a.isupper())
print(b.isupper())

结果:

False
True

(15)istitle():判断是否为标题
代码:

a = "Hello World!"
b = "hello world!"
print(a.istitle())
print(b.istitle())

结果:

True
False

(16)isdigit():判断字符串是否全部为数字。
代码:

a = "Hello World!"
b = "123456"
print(a.isdigit())
print(b.isdigit())

结果:

False
True

(17)isspace():判断字符串是否全为空格。
代码:

a = "Hello World!"
b = "            "
print(a.isspace())
print(b.isspace())

结果:

False
True

(18)isalpha():判断字符串是否全为字符。
代码:

a = "Hello World!"
b = "helloworld"
print(a.isalpha())
print(b.isalpha())

结果:

False
True

(19)title():把字符串转化为标题格式。
代码:

a = "hello world!"
print(a.title())

结果:

Hello World!

(20)lower():将字符串的字母转化为小写。
代码:

a = "Hello World!"
b = a.lower()
print(b)

结果:

hello world!

(21)upper():将字符串中的所有字母转化为大写。
代码:

a = "Hello World!"
b = a.upper()
print(b)

结果:

HELLO WORLD!

(22)split():在指定的分隔符处拆分字符串,返回的是列表。
代码:

a = "Hello World!"
b = a.split(" ")
print(b)

结果:

['Hello', 'World!']

(23)join():按照指定的格式将一个可迭代对象拼接为字符。
代码:

a = ("Hello","World","!")
b = "*".join(a)
print(b)

结果:

Hello*World*!

(24)strip():清除字符串两侧空格。
代码:

a = "     Hello World!    "
b = a.strip()
print(b)

结果:

Hello World!

(25)lstrip():清除字符串左侧的空格。
代码:

a = "     Hello World!    "
b = a.lstrip()
print(b)

结果:

Hello World!    

(26)rstrip():清除字符串右侧的空格。
代码:

a = "     Hello World!    "
b = a.rstrip()
print(b)

结果:

     Hello World!

(27)replace():用新的字符替换旧的字符。
代码:

a = "     Hello World!    "
b = a.replace(" ","*")
print(b)

结果:

*****Hello*World!****

(28)ljust():左对齐。
代码:

a = "Hello World"
b = a.ljust(20)
print(b,"!")

结果:

Hello World          !

(29)rjust():右对齐。
代码:

a = "Hello World"
b = a.rjust(20)
print(b,"!")

结果:

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值