Working with strings · Làm việc với chuỗi ký tự
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.
Các chữ cái có vị trí
- Một chuỗi là một hàng ký tự.
- Mỗi ký tự có một vị trí, được gọi là chỉ số.
- Chỉ số bắt đầu từ
0. Sử dụngs[-1]để trỏ đến ký tự cuối cùng.
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:].
Lấy một đoạn con
- Một đoạn con (slice) lấy một phần của chuỗi:
s[start:end]. - Ký tự tại
endkhông được bao gồm. - Để trống một bên để có nghĩa "từ đầu" hoặc "cho đến cuối":
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.
Ví dụ minh họa: vị trí và đoạn con
- Trong
"banana"các vị trí làb=0,a=1,n=2,a=3,n=4,a=5. word[0]là chữ cái đầu tiên;word[-1]đếm ngược từ cuối.word[:3]lấy các vị trí 0, 1 và 2, và dừng lại trước 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.
Độ dài và thay đổi hoa/thường
len(s)trả về số lượng ký tự.s.upper()tạo ra một bản sao HOA;s.lower()tạo ra một bản thường.- Những hàm này tạo ra một chuỗi mới; chuỗi gốc không thay đổi.
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-strings xây dựng văn bản
- Một f-string bắt đầu bằng
ftrước dấu ngoặc kép. - Đặt một giá trị bên trong
{ }và Python sẽ chèn nó vào văn bản. - Đây là cách gọn gàng để kết hợp từ và giá trị.
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
+.
Lỗi thường gặp
- Chỉ số bắt đầu từ
0;s[-1]là ký tự cuối cùng. - Chuỗi không thể bị thay đổi tại chỗ — các phương thức như
.upper()trả về MỘT chuỗi MỚI, vì vậy hãy lưu kết quả. - Bạn chỉ có thể nối một chuỗi với một chuỗi khác bằng
+.
Now you try
- Read the task to see which variable to fill or what to print.
- Press Check answer to test your code.
Bây giờ bạn thử
- Đọc yêu cầu bài tập để xem biến nào cần điền hoặc phải in gì.
- Nhấn Check answer (Kiểm tra câu trả lời) để thử mã của bạn.
Index & slice a string · Truy vấn & cắt xén một chuỗi
Each character has an index from 0; a slice s[a:b] takes a range. · Mỗi ký tự có một chỉ mục bắt đầu từ 0; một cắt xén s[a:b] lấy một khoảng giá trị.
word is "computer". Store its first character in first. Indexes start at 0, so that character is word[0]. · word là "computer". Lưu ký tự đầu tiên của nó vào first. Các chỉ mục bắt đầu từ 0, nên ký tự đó có chỉ mục là word[0].
Click Run to see the output here. · Nhấn Chạy để xem kết quả ở đây.
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 đã là ký tự đầu tiên của "computer". Lưu bốn ký tự đầu tiên vào first_four bằng cách cắt xén. word[a:b] dừng lại trước b, nên bốn ký tự tương ứng là word[:4].
Click Run to see the output here. · Nhấn Chạy để xem kết quả ở đây.
phrase is "Hello World". Store its length in n with len(...), and an UPPERCASE copy in loud with .upper(). · phrase là "Hello World". Lưu độ dài của nó vào n bằng len(...), và một bản sao HOA vào loud bằng .upper().
Click Run to see the output here. · Nhấn Chạy để xem kết quả ở đây.
name is "Mia" and age is 12. Use an f-string to print exactly: Mia is 12 years old. · name là "Mia" và age là 12. Sử dụng chuỗi định dạng (f-string) để in chính xác: Mia is 12 years old.
Click Run to see the output here. · Nhấn Chạy để xem kết quả ở đây.
first is "ada" and last is "lovelace". Store the initials in initials as capital letters, each followed by a dot: "A.L.". · first là "ada" và last là "lovelace". Lưu viết tắt tên vào initials dưới dạng chữ hoa, mỗi chữ cái theo sau là một dấu chấm: "A.L.".
Click Run to see the output here. · Nhấn Chạy để xem kết quả ở đây.