我们在之前已经接触过字符串,它在 python 是str
类型。现在让我我们来更深入地了解一下。
一个字符串是一系列字符的组合,因为我们可以通过方括号
操作符号加上索引来获取单个字符。
words = '小明爱吃瓜'
print(words[1])
输出
明
咦,第一个字符不是小
吗?为什么这里打印了明
,我的 python 坏掉了吗?
你的 python 没有坏掉,因为在绝大多数包括 python 的程序语言中,计数都是从 0
开始的。
这意味着我们的索引0
才能拿到小
字。
words = '小明爱吃瓜'
print(words[1])
输出
明
我们取最后一个字符岂不是要先数下字符串有多少个字符,如果字符串是一篇论文,我们不是要数到脑壳疼?
这样子的话让 python 帮我们数吧,内建的len
函数可以帮我们数出字符串的长度。由于索引从 0 开始,长度减 1 就是我们最后一个字符的索引。
words = '小明爱吃瓜'
length = len(words)
print('长度:', length)
print('最后一个元素是:', words[length-1])
输出
长度: 5
最后一个元素是: 瓜
拿最后一个字符除了数长度之外我们还有更好的办法,就是用 -1
作为索引。
因为 python 里同时有另一种索引方式,那就是 -1 作为最后一个元素的索引,往前逐个递减。
words = '小明爱吃瓜'
print('最后一个元素是:', words[-1])
输出
最后一个元素是: 瓜
字符串中的一部分叫作切片,例如"爱吃"
相对于"小明爱吃瓜"
。我们同样可以用方括号操作符去获取字符串的切片, 获取方式为 [a:b]
。其中 a
为起始索引,b
为结束索引。
注意的是获取的切片包含 a
不包含 b
。
例如要获取"爱吃"
。
words = '小明爱吃瓜'
sliced = words[2:4]
print(sliced)
输出
爱吃
当我们将 a
置为空时,切片结果为 b
之前不包含 b
的所有字符。当我们将 b
置为空时,切片的结果就为 a
之后并包含 a
的所有字符。
words = '小明爱吃瓜'
print(words[:4])
print(words[2:])
输出
小明爱吃
爱吃瓜
当 a
等于 b
时返回结果为空字符串,当 a
与 b
都置空时返回当前字符串的拷贝对象。
words = '小明爱吃瓜'
print(words[2:2])
print(words[:])
输出
小明爱吃瓜
in
操作符可以左右各接收一个字符串,判断左边的字符串是否被包含在右边的字符串中,返回结果是一个 bool
对象。
words = '小明爱吃瓜'
print('小明' in words)
print('小红' in words)
输出
True
False
我们依旧可以用dir
查看字符串中有哪些可用的函数,同时用 help
查看其介绍。
words = '小明爱吃瓜'
print(dir(words))
print(help(words.upper))
输出
['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isascii', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
Help on built-in function upper:
upper() method of builtins.str instance
Return a copy of the string converted to uppercase.
我们介绍几个常用的方法。首先是upper
,将字符串的字母转换成大写。
words = 'happy'
print(words.upper())
输出
HAPPY
很显示,对应的 lower
就是小写。
words = 'HAPPY'
print(words.lower())
输出
happy
isdigit
的用法是判断字符串是否是书面意思上的数字。
words = '10'
print(words.isdigit())
words = '10ab'
print(words.isdigit())
help(str.isdigit)
输出
True
False
Help on method_descriptor:
isdigit(self, /)
Return True if the string is a digit string, False otherwise.
A string is a digit string if all characters in the string are digits and there
is at least one character in the string.
很多内置函数的学习可以用 help
函数去查看和了解。建议同学们可以经常使用,这里就不过多赘述了。
常见的数字类型包括 int
(整型)和 float
(浮点型)。在之前我们已经用 str
类型来表示我们生活中的一些常见内容了,比如姓名、地址、小说等我们都用 str
类型进行保存。
我们还需要一些类型,来表示我们的体重、年龄、商品的价格等数据,并且能对他们进行数学上的运算。这时就需要int
和 float
类型。为什么需要定义两种类型呢?
因为对于 int
(整型)的数据,它可以用来表达次数的概念。我去了两次北京,而不能说我去了 2.5 次北京。因此在使用上整数与带小数的数字必须有所区分,所以把数字分成 int
与 float
两个类型。
当我们给变量赋值一个不带小数点的数字时,这个变量就默认为int
(整型)。
n = 12
print(type(n))
输出
<class 'int'>
注意,两个整型经过除法运算后,不管结果是不是意义上的整数,其都为 float
(浮点型)。
n = 4 / 2
print(type(n))
输出
<class 'float'>
一个数字只有带有 .
都为浮点型。
f1 = 1.5
f2 = 1.
f3 = .5
print(f1, type(f1))
print(f2, type(f2))
print(f3, type(f3))
输出
1.5 <class 'float'>
1.0 <class 'float'>
0.5 <class 'float'>
- 定义姓名、年龄、身高三个变量来保存关于你自身的数据,你能说出他们分别属于什么类型吗?
- 打印出你姓名中的姓,假设你是
慕容XX
复姓的话又该如何?