Working with strings · 处理字符串
Letters have positions
- A string is a row of characters.
- Each character has a position, called an index.
- Indexes start at
0. Uses[-1]for the last character.
字母有位置
- 字符串是一串字符(character)。
- 每个字符都有一个位置,叫作索引(index)。
- 索引从
0开始。用s[-1]表示最后一个字符。
word = "Python"
print(word[0])
print(word[-1])
Take a slice
- A slice takes part of a string:
s[start:end]. - The character at
endis not included. - Leave a side empty for "from the start" or "to the end":
s[:3],s[2:].
取一段切片
- 切片(slice)从字符串中取出一部分:
s[start:end]。 - 位置
end处的字符不包含在内。 - 留空一侧表示"从头开始"或"到末尾":
s[:3]、s[2:]。
msg = "hello"
print(msg[:3])
print(msg[2:])
Worked example: positions and slices
- In
"banana"the positions areb=0,a=1,n=2,a=3,n=4,a=5. word[0]is the first letter;word[-1]counts back from the end.word[:3]takes positions 0, 1 and 2, and stops before 3.
例题:位置和切片
- 在
"banana"里,各个位置是b=0、a=1、n=2、a=3、n=4、a=5。 word[0]是第一个字母;word[-1]从末尾往回数。word[:3]取位置 0、1、2,在 3 之前停下。
word = "banana"
print(word[0]) # b
print(word[-1]) # a (the last letter)
print(word[:3]) # ban (positions 0, 1, 2)
print(word[2:5]) # nan (positions 2, 3, 4)
Length and changing case
len(s)gives the number of characters.s.upper()makes a CAPITAL copy;s.lower()makes a lowercase copy.- These make a new string; the original does not change.
长度与大小写
len(s)返回字符的个数。s.upper()生成一个全大写的副本;s.lower()生成一个全小写的副本。- 它们会生成新的字符串;原来的字符串不会改变。
city = "Tokyo"
print(len(city))
print(city.upper())
print(city.lower())
f-strings build text
- An f-string starts with
fbefore the quote. - Put a value inside
{ }and Python drops it into the text. - It is the neat way to mix words and values.
用 f-string 拼接文字
- f-string 在引号前加一个
f。 - 在
{ }里放一个值,Python 会把它填进文字中。 - 这是把文字和值混在一起的简洁写法。
fruit = "apple"
count = 3
print(f"I have {count} {fruit}s")
Common mistakes
- Indexing starts at
0;s[-1]is the last character. - Strings cannot be changed in place — methods like
.upper()return a NEW string, so store the result. - You can only join a string to a string with
+.
常见错误
- 索引从
0开始;s[-1]是最后一个字符。 - 字符串不能原地修改——
.upper()等方法返回新字符串,要把结果存下来。 - 用
+只能把字符串和字符串拼接。
Now you try
- Read the task to see which variable to fill or what to print.
- Press Check answer to test your code.
现在轮到你
- 读题,看清要填哪个变量或要打印什么。
- 按检查答案来测试你的代码。
Index & slice a string · 字符串的索引与切片
Each character has an index from 0; a slice · 切片 s[a:b] takes a range. · 每个字符都有从 0 开始的索引;切片 s[a:b] 取一段范围。
word is "computer". Store its first · 第一个 character in first. Indexes start at 0, so that character is word[0]. · word 是 "computer"。把它的第一个字符存进 first。索引从 0 开始,所以这个字符是 word[0]。
Click Run to see the output here. · 点击“运行”查看此处输出。
first is already the first character of "computer". Store the first four characters in first_four with a slice. word[a:b] stops before b, so four characters is word[:4]. · first 已经是 "computer" 的第一个字符。用切片把前四个字符存进 first_four。word[a:b] 在 b 之前停下,所以四个字符是 word[:4]。
Click Run to see the output here. · 点击“运行”查看此处输出。
phrase is "Hello World". Store its length in n with len(...), and an UPPERCASE copy in loud with .upper(). · phrase 是 "Hello World"。用 len(...) 把它的长度存进 n,用 .upper() 把它的全大写副本存进 loud。
Click Run to see the output here. · 点击“运行”查看此处输出。
name is "Mia" and age is 12. Use an f-string to print exactly: Mia is 12 years old. · name 是 "Mia",age 是 12。用 f-string 精确打印:Mia is 12 years old.
Click Run to see the output here. · 点击“运行”查看此处输出。
first is "ada" and last is "lovelace". Store the initials in initials as capital letters, each followed by a dot: "A.L.". · first 是 "ada",last 是 "lovelace"。取两个名字的首字母,变成大写,每个后面加一个点,存进 initials:"A.L."。
Click Run to see the output here. · 点击“运行”查看此处输出。