python学习--字符串

1.字符串的定义

字符串本身是不可变的,与元组类似。

str1 = 'hello world'  # 单行字符串

str2 = "hello world"  # 单行字符串

str3 = '''hello
 world'''  # 多行字符串

str4 = """hello
 world"""  # 多行字符串

'因此单行字符串可以拿来单行注释'

"""多行字符串
也可以拿来
多行注释"""

2.字符串的函数

2.1字符串的替换

字符串本身是不可替换的。但是可以用replace函数将要替换的结果输出为其他字符串。
replace函数将要改的字符放在第一个参数的位置,改的结果放在第二个,返回值是字符串类型。

str5 = str1.replace('h', 'a')
print(str5)  # aello world

当然replace函数也可以指定需要修改的个数,默认从左往右修改

str6 = str1.replace('l', 'c', 1)
print(str6)  # heclo world
str7 = str1.replace('l', 'c', 2)
print(str7)  # hecco world

2.2字符串的大小写

字符串难免会有大小写之分,python内置部分方法可以进行大小写变化。
既然有大小写肯定得全是字符啦。

str8 = str1.upper()  # 字符串全大写
print(str8)  # HELLO WORLD
str9 = str8.lower()  # 字符串全小写
print(str9)  # hello world
str10 = str1.capitalize()  # 首字母大写
print(str10)  # Hello world
str11 = str1.title()  # 标题形式,每个单词首字母大写
print(str11)  # Hello World

2.3字符串的删除

字符串有些时候在其左右两边会出现一些没有意义的空白格,python也有内置函数用于删除这些空格,对爬虫很有用。

str1 = "    hello world     "
str2 = str1.strip()  # 去掉两边的空格
print(str2)  # hello world
str2 = str1.lstrip()  # 去掉左边的空格
print(str2)  # hello world     
str2 = str1.rstrip()  # 去掉右边的空格
print(str2)  # hello world

2.4字符串的切割

字符串可以实现按空格切割或者按指定字符串切割,返回值是列表。

str1 = 'hello world this is python'
str2 = str1.split()  # 默认以空格作为切割符
print(str2)  # ['hello', 'world', 'this', 'is', 'python']
str2 = str1.split('o')  # 以字符o为切割符,也可以为字符串
print(str2)  # ['hell', ' w', 'rld this is pyth', 'n']
str2 = str1.split('o', 2)  # 限定切割的次数
print(str2)  # ['hell', ' w', 'rld this is python']

2.5字符串的拼接

字符串是可以通过join,format等方式拼接起来的
join的传入值是列表,并’ '里的内容拼接

str1 = 'hello'
str2 = 'world'
str3 = ''.join([str1, str2])  # ''里表示用什么字符串进行连接,无内容则直接连接
print(str3)  # helloworld
str3 = ' '.join([str1, str2])  # 用空格连接
print(str3)  # hello world

结合已学知识还有

str1 = 'hello world this is python'
str2 = ''.join(str1.split('this'))  # join可以实现字符串的删除
print(str2)  # hello world  is python
str2 = 'he'.join(str1.split('this'))  # join可以实现字符串的替换
print(str2)  # hello world he is python

2.6字符串的查找

字符串可以通过字符查找其对应的位置

str1 = 'hello world this is python'
a = str1.index('h')  # 从0开始返回最近的'h'的下标
print(a)  # 0
a = str1.index('h', 2)  # 从2开始返回最近的'h'的下标
print(a)  # 13
a = str1.index('h', 100)  # 当开始值超过字符串长度或者找不到该字符,程序会报错

index函数的缺点就是有可能会出现报错情况导致程序停止运行,因此有了find函数

a = str1.find('h', 100)  # 找不到则返回-1
print(a)  # -1

2.7字符串的其他函数

str1 = 'hello'
a = str1.isalpha()  # 判断是否全是英文字母
b = str1.isdigit()  # 判断是否全是数字
c = str1.isupper()  # 判断是否全大写
e = str1.islower()  # 判断是否全小写

2.7字符串的编码

字符串有gbk、utf-8等编码方式可以对字符串编码和解码。对爬虫很有用。

str1 = 'Ice-冰鸽'
bstr1 = str1.encode('utf-8')  # 以utf-8编码
print(bstr1)  # b'Ice-\xe5\x86\xb0\xe9\xb8\xbd'
str2 = bstr1.decode('utf-8')  # 以utf-8解码
print(str2)  # Ice-冰鸽

需要注意的是编码方式要与解码方式一致否则会出现熟悉的乱码,或者程序报错。

3.结束语

大家喜欢的话可以点赞+转发哦,我将继续更新后续的python学习。

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值