Python最常用的数据类型——字符串

字符串就是一串字符,是编程语言中表示文本的数据类型。

字符串是 Python 中最常用的数据类型。

字符串的声明

在 Python 中可以使用一对双引号"或者一对单引号'定义一个字符串。

那么在开发时,是使用双引号还是使用单引号呢?

首先呢,大多数编程语言都是用双引号来定义字符串,所以我们在开发时更倾向使用双引号,这样可以和其他编程语言相通。

虽然可以使用\"或者\'做字符串的转义,但是在实际开发中:
如果字符串内部需要使用双引号,那么可以使用单引号定义字符串。
如果字符串内部需要使用单引号,可以使用双引号定义字符串。

str1 = '我的外号是"大西瓜"'
print(str1)
# 我的外号是"大西瓜"

三引号声明字符串变量

还有一种特别的情况,就是需要声明的字符串中包含换行符、制表符、以及其他特殊字符的时候,我们可以使用三引号来进行赋值。

Python 中三引号声明变量就是针对复杂的字符串。

三引号的语法是一对连续的单引号或者双引号(通常都是成对的用)。

hi = '''hi 
there'''
print(hi)
# hi 
# there

三引号声明字符串可以自始至终保持一小块字符串的格式是所谓的所见即所得格式的。

一个典型的用例是,当你需要一块HTML或者SQL时,这时用三引号标记非常方便,使用传统的转义字符体系将十分费神。

扫描二维码关注公众号,回复: 11665727 查看本文章

字符串索引概念

之前学习列表元祖都有索引的概念,我们同样可以把字符串看成是存储多个字符的大柜子,在每个小格子中存储一个又一个字符。

所以我们同样可以使用索引获取一个字符串中指定位置的字符,索引计数也是从0开始的。

字符串索引示意图

str1 = "hello python"
print(str1[6])
# p

我们也同样可以使用 for 循环通过迭代遍历的方式读取字符串中每一个字符。

实例

string = "Hello Python"
for char in string:
    print(c)

字符串的常用操作

python 提供的字符串操作方式特别多,但正是因为 python 提供的方法足够多,才使得在开发时,能够针对字符串进行更加灵活的操作,应对更多的开发需求!

这里只介绍几个常用的字符串方法,剩余的在实际开发中用到的时候翻阅手册即可,多用就记住了。

len 统计字符串长度

hello_str = "hello hello"
print(len(hello_str))
# 11

count 统计某一个小(子)字符串出现的次数

hello_str = "hello hello"
print(hello_str.count("llo"))
print(hello_str.count("abc"))
# 2
# 0 子串不存在时,程序不报错。

isspace 判断空白字符。

space_str = "      \t\n\r"
print(space_str.isspace())
# True

判断字符串中是否只包含数字。

num_str = "1.1"
# 都不能判断小数
print(num_str.isdecimal()) # False
print(num_str.isdigit()) # False
print(num_str.isnumeric()) # False
# unicode 字符串
num_str = "\u00b2"
print(num_str.isdecimal()) # False
print(num_str.isdigit()) # True
print(num_str.isnumeric()) # True
# 中文数字
num_str = "一千零一"
print(num_str.isdecimal()) # False
print(num_str.isdigit()) # False
print(num_str.isnumeric()) # True

startswith 判断是否以指定字符串开始

hello_str = "hello world"
print(hello_str.startswith("Hello"))
# False

endswith 判断是否以指定字符串结束

hello_str = "hello world"
print(hello_str.endswith("world"))
# True

index 查找某一个子字符串出现的位置

hello_str = "hello hello"
print(hello_str.index("llo"))
# 2
# 使用index方法传递的子字符串不存在,程序会报错
print(hello_str.index("abc"))
# ValueError: substring not found

find 查找某一个子字符串出现的位置

hello_str = "hello world"
print(hello_str.find("llo"))
# 2
# find如果指定的字符串不存在,会返回-1
print(hello_str.find("abc"))
# -1

replace 替换字符串

hello_str = "hello world"
print(hello_str.replace("world", "python"))
# hello python
print(hello_str)
# hello world

replace方法执行完成之后,会返回一个新的字符串,不会修改原有字符串的内容,所以需要一个变量去接收。

strip 删除字符串中多余的空格和特殊字符

str = "  www.daydaylearn.cn \t\n\r"
print(str)
str2 = str.strip()
# 并不会改变字符串本身,需要新的变量接收。
print(str2)
#  www.daydaylearn.cn

#www.daydaylearn.cn

在 str.strip([chars]) 中,[chars] 用来指定要删除的字符,可以同时指定多个,如果不手动指定,则默认会删除空格以及制表符、回车符、换行符等特殊字符。

split 拆分字符串

poem_str = "登鹳雀楼\t 王之涣 \t 白日依山尽 \t \n 黄河入海流 \t\t 欲穷千里目 \t\t\n更上一层楼"
poem_list = poem_str.split()
print(poem_list)
# ['登鹳雀楼', '王之涣', '白日依山尽', '黄河入海流', '欲穷千里目', '更上一层楼']

join 合并字符串

poem_list = ['登鹳雀楼', '王之涣', '白日依山尽', '黄河入海流', '欲穷千里目', '更上一层楼']

result = " ".join(poem_list)
print(result)
# 登鹳雀楼 王之涣 白日依山尽 黄河入海流 欲穷千里目 更上一层楼

编程的朝圣之路

猜你喜欢

转载自blog.csdn.net/beyondamos/article/details/108054159
今日推荐