So I want to introduce one more operation on strings, which
we'll find very useful, which is the Find operation. It gives
us the way in a big string to find some sub-string
that we're looking for. The way we use Find is a little
different from the way we've used other operators so far. Because
Find is actually a method, and what that means is it's
a built in procedure provided by Python. We'll be able to
define our own procedures soon, we'll get to that in unit two.
Find is a procedure that operates on strings, so we use it by having
a string followed by .find, followed by
a parentheses, then we pass in another string.
Which is the string that we want to find in
the first string. And the output of Find is the
position in the string where that sub-string is found, the
first occurrence of the string. So, if that string happens
to occur in more places than one in the
input string, the result of find is always going to give
us the position. That's the number where the first occurrence
of the sub-string occurs. So the output of using Find
will be the first position in the search string, which
is this blue string right here, where the target string,
which is the purple string, occurs. So that will be
a number. If the target string is not a found anywhere
in the search string, then the output would be negative
1. So let's try a few examples to understand how
that works and we'll do this in the Python interpreter.
Here I've initialized the variable Pythagoras to hold the string here
that's been attributed to Pythagoras. We don't know if
he really said it. But it says there's a geometry
in the humming of strings, there is music in
the spacing of spheres. So now, we have that variable
initialized, so I'm going to invoke Find, using Pythagoras as
the string that we're searching in, and that's the value
that we initialized it to with a string, passing in
as the search string the string string. When we run
this, we see that we get 40 as the result. If we counted, this is position 0, we
would see string starting at position 40. Since I
don't want to count that far, we can use our indexing
to see if that's right. So let's print Pythagoras
starting from index 40, we could print all the
way to the end using a colon. And, when
we run that, we see that it starts with string
which is what we found with the Find. We can search
for other positions if we search for Pythagoras the single letter T.
Well that matches the beginning, so we should find the resulted position
0 which is what we get and we can look for sphere.
[BLANK_AUDIO]
That will match sphere at the end. We get
position 86. Let's print the quote from position 86. And
we see the end of the quote starting from sphere. [SOUND]
If we search for a string that's not in the string
that we're using as the search string, so let's look for
say, algebra, which was not in the quote from Pythagoras, we
get the output negative one. That means the string was not found.
文字列の操作をもう1つ紹介します
find操作といいとても有用です
大きな文字列の中のサブ文字列を見つけます
findの使い方はこれまで他の演算子で
行ってきた使い方とは少し異なります
findはメソッドだからです
つまりPythonが提供する組み込み機能です
あとのレッスン2で独自の機能を定義します
findは文字を操作する処理です 文字列のあとに
.find さらにその後ろに丸括弧をつけます
そして別の文字列を渡します
最初の文字列で見つけたい文字列です
findの出力はサブ文字列が
見つかった文字列の位置 つまり文字列が
最初に出現した位置になります
複数の文字列が出現する場合
findの結果から常に位置が分かります
つまりサブ文字列が最初に出現した数です
ですからfindを使った出力は
検索文字列の1つ目の位置になります
ここの青色の文字列です
ターゲット文字列が出現している場合は
紫色の文字列です
それが数値です ターゲット文字列が検索文字列で
見つからない場合出力は-1になります
どう機能するか理解するために
Pythonインタプリタで試しましょう
変数pythagorasに
この文字列を保持していると設定しました
真偽はともかくピタゴラスが言ったとされる言葉です
“弦の響きには幾何学があり
天空の配置には音楽がある”
変数を設定できたので
文字列のpythagorasを使って
findを呼び出します
文字列として設定した値です
検索文字列としてstringを渡します
これを実行すると結果が40と確認できます
ここがゼロです
40の位置で始まる文字列を確認できます
これ以上数えるのはやめます
インデックスを使用してindex40から始まる
pythagorasを表示させます
コロンを使うと最後まで表示できます
実行するとfindで見つけた文字列で
始まることが確認できます
pythagorasのTを検索したい場合は
別の位置を探します
最初と合致しました
なのでここからゼロの位置を見つけます
ここまでできました 次にsphereを探します
末尾にあるsphereと合致しました
86の位置を取得したのでその位置の言葉を表示します
sphereから始まる言葉の最後が確認できます
検索文字列にない文字列を検索しましょう
何にしましょうか
algebraにしましょう
ピタゴラスの言葉にはありません
-1となり文字列は見つかりませんでした
接下来我想介绍关于字符串的另一项操作
这项操作非常实用 它就是 Find 运算
它可以帮助我们在一个大字符串中找到我们想要的某一个子字符串
我们操作 Find 的方式与目前我们操作其他运算符的方式稍有不同
因为 Find 实际上是一种方法
是在由 Python 提供的程序中使用的
我们很快就能自己定义自己的程序
在第二单元会学习这部分内容
Find 是对字符串进行操作的一个程序
因此使用的时候是在一个字符串之后加上 .find
再之后是括号 然后是另一个字符串
这个字符串就是我们想要在第一个字符串中找到的那个字符串
Find 输出的是
子字符串在字符串中的位置
是子字符串第一次出现的位置
因此 如果该字符串在输入字符串中出现多次
find 输出的结果将始终是位置
是子字符串第一次出现的那个位置的数字
因此使用 Find 输出的是
在搜索字符串中出现的第一个位置
就是这里的这个蓝色的字符串
它就是目标字符串出现的位置 目标字符串是紫色的
因此出现的是一个数字
如果在搜索字符串中没有发现目标字符串
则输出结果显示的是负1 下面来试几个例子帮助我们理解 Find 具体是怎么运作的
我们来在 Python 解释器中来进行操作
这里 我已经设置了初始变量 Pythagoras
使变量 Pythagoras 等于这里列出的字符串 他是否真的说过这句话我们并不知晓
但这句话是 there's a geometry in the humming of strings,
there is music in the spacing of spheres
现在 我们已经初始化了变量
接下来我要调用 Find 来进行操作
将 Pythagoras 作为我们用于进行搜索的字符串
Pythagoras 的值是用刚刚提到的字符串进行初始化设定
输入要搜索的字符串 string 字符串
运行之后 我们得到的结果是40 如果我们数一下的话 这是初始位置0
我们将会看到 string 开始于第40个位置
不过我不想数那么远 我们可以利用索引来看一下该位置是否正确
我们输出 Pythagoras 从第40个位置开始索引
我们可以用冒号输出
从第40个到最后一个字符的所有内容
运行时 我们看到输出的内容是以 string 开头的
这正是我们用 Find 查找到的内容
我们也可以查找其他位置 如果我们在 Pythagoras 中搜索单个字母 T
实际上在开头部分就会搜索到字母 T 因此我们发现起始位置0就是其所在位置
我们可以搜索 sphere
[无声音]
运行之后将会在字符串末尾 第86个位置找到相匹配的 sphere
我们输出从第86个位置开始的字符串
我们会看到最终显示的字符以 sphere 开头
如果查找的这个字符并未出现在
我们用于搜索的字符串中
例如 我们搜索 algebra 这个字符 该字符并未出现在 Pythagoras 的引文中
运行之后我们得到负1 这说明未查找到该字符串