Working with strings
Python Basics Lesson 5 3:01 English narration · English + 中文 subtitles burned in
Chapters
Transcript
A string is a row of characters, and each one sits in a numbered position called an index.
字符串是一排字符,每个字符都待在一个编号的位置上,这个编号叫索引。
Here is the surprise that trips up every beginner: counting starts at zero, not one.
有一点会让每个初学者绊一下:计数从 0 开始,不是从 1 开始。
So the capital P of Python is at position zero, and the n at the end is at position five.
所以 Python 的大写 P 在位置 0,末尾的 n 在位置 5。
Counting backwards is easier: minus one is always the last character, whatever the length.
倒着数更方便:不管字符串多长,-1 永远是最后一个字符。
To take one character, put the position in square brackets after the name.
要取出某一个字符,就在名字后面用方括号写上位置。
Two positions with a colon between them take a piece — a slice.
两个位置中间加一个冒号,取出的是一段——叫切片。
The first three characters of hello are h, e, l: written colon three, because a slice stops just BEFORE the second number.
hello 的前三个字符是 h、e、l,写作 ,因为切片会在第二个数字之前停下。
That off-by-one is deliberate, and it means the numbers never overlap when you cut a string twice.
这个差一的设计是故意的:这样你把一个字符串切两刀时,编号不会重叠。
Leave a side empty for from the start or to the end: two colon runs from two to the end and gives l, l, o.
某一边留空表示“从头开始”或“到末尾”: 从位置 2 一直到结尾,给出 l、l、o。
Three tools you will use constantly.
三个你会经常用到的工具。
len, with the string in brackets, says how many characters there are — Tokyo has five.
len 加上括号里的字符串,告诉你有多少个字符——Tokyo 是 5。
Dot upper gives you a capital copy, and dot lower gives you a lowercase copy.
.upper() 给你一个全大写的副本,.lower() 给你一个全小写的副本。
Read that word copy carefully: the original does not change.
注意“副本”这个词:原来的字符串并不会改变。
If you want the loud version, you have to store what comes back, or it is thrown away the moment the line ends.
如果你要那个大写的版本,就必须把返回的结果存起来, 否则这一行结束时它就被丢掉了。
Mixing words and values can be done the hard way — join with plus, convert every number, and count your spaces by eye.
把文字和值混在一起,可以用麻烦的办法——用加号拼接,把每个数字转换, 再用眼睛数空格。
Or you can put the letter f immediately before the opening quotation mark.
也可以在开头的引号前面紧挨着写一个字母 f。
Then anything inside curly brackets is worked out and dropped into the sentence where it stands.
这样,花括号里的东西会被算出来,并放进句子里它所在的位置。
Same result, one line, and you can see the finished sentence while you write it.
结果一样,只要一行,而且你在写的时候就能看出整句话长什么样。
Now the lesson's first task, on the word computer.
现在做课程里的第一道题,用单词 computer。
It wants the first character, and the first four.
它要第一个字符,以及前四个字符。
The first character is position zero — square brackets, zero — and that is the letter c.
第一个字符是位置 0——方括号里写 0——也就是字母 c。
The first four is a slice with an empty left side: colon four.
前四个是一个左边留空的切片: 。
It runs up to four without including it, so you get c, o, m, p.
它一直取到 4,但不包含 4, 所以你得到 c、o、m、p。
Look at the ruler: position four is the letter u, and it is not in the answer.
看那把标尺:位置 4 是字母 u,它不在答案里。
Four things to take with you.
带走四点。
One: indexes start at zero, and minus one is the last character.
第一:索引从 0 开始,-1 是最后一个字符。
Two: a slice stops before its second number.
第二:切片会在第二个数字之前停下。
Three: upper and lower return a new string, so store it if you want to keep it.
第三:upper 和 lower 返回的是新字符串,想留住就要存起来。
Four: an f-string drops a value straight into the words.
第四:f-字符串能把值直接放进句子里。
Now do the three tasks — the third one wants an f-string, and the full stop at the end matters.
现在去做那三道题——第三题要用 f-字符串,末尾的句号也算数。