栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python学习--字符串

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python学习--字符串

字符串
  • 1.字符串的定义
  • 2.字符串的函数
    • 2.1字符串的替换
    • 2.2字符串的大小写
    • 2.3字符串的删除
    • 2.4字符串的切割
    • 2.5字符串的拼接
    • 2.6字符串的查找
    • 2.7字符串的其他函数
    • 2.7字符串的编码
  • 3.结束语

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-xe5x86xb0xe9xb8xbd'
str2 = bstr1.decode('utf-8')  # 以utf-8解码
print(str2)  # Ice-冰鸽

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

3.结束语

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/529252.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号