Working with strings · Bekerja dengan string
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.
Huruf memiliki posisi
- String adalah barisan karakter.
- Setiap karakter memiliki posisi, disebut indeks.
- Indeks dimulai dari
0. Gunakans[-1]untuk karakter terakhir.
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:].
Mengambil potongan (slice)
- Potongan (slice) mengambil sebagian string:
s[start:end]. - Karakter pada
endtidak termasuk. - Biarkan sisi kosong untuk "dari awal" atau "hingga akhir":
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.
Contoh terpecahkan: posisi dan potongan
- Di
"banana"posisinya adalahb=0,a=1,n=2,a=3,n=4,a=5. word[0]adalah huruf pertama;word[-1]menghitung mundur dari akhir.word[:3]mengambil posisi 0, 1, dan 2, dan berhenti sebelum 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.
Panjang dan mengubah kasus huruf
len(s)memberikan jumlah karakter.s.upper()membuat salinan HURUF BESAR;s.lower()membuat salinan huruf kecil.- Ini membuat string baru; string asli tidak berubah.
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 membangun teks
- f-string dimulai dengan
fsebelum tanda kutip. - Masukkan nilai ke dalam
{ }dan Python akan menyisipkannya ke dalam teks. - Ini adalah cara rapi untuk mencampur kata dan nilai.
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
+.
Kesalahan umum
- Pengindeksan dimulai dari
0;s[-1]adalah karakter terakhir. - String tidak dapat diubah in-place — method seperti
.upper()mengembalikan STRING BARU, jadi simpan hasilnya. - Anda hanya dapat menggabungkan string dengan string menggunakan
+.
Now you try
- Read the task to see which variable to fill or what to print.
- Press Check answer to test your code.
Sekarang Anda coba
- Bacalah tugas untuk melihat variabel mana yang harus diisi atau apa yang harus dicetak.
- Tekan Periksa jawaban untuk menguji kode Anda.
Index & slice a string · Indeks & iris string
Each character has an index from 0; a slice s[a:b] takes a range. · Setiap karakter memiliki indeks mulai dari 0; iris s[a:b] mengambil rentang.
word is "computer". Store its first character in first. Indexes start at 0, so that character is word[0]. · word adalah "computer". Simpan karakter pertamanya dalam first. Indeks dimulai dari 0, jadi karakter tersebut adalah word[0].
Click Run to see the output here. · Klik Jalankan untuk melihat output di sini.
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 sudah merupakan karakter pertama dari "computer". Simpan empat karakter pertama dalam first_four dengan iris. word[a:b] berhenti sebelum b, jadi empat karakter adalah word[:4].
Click Run to see the output here. · Klik Jalankan untuk melihat output di sini.
phrase is "Hello World". Store its length in n with len(...), and an UPPERCASE copy in loud with .upper(). · phrase adalah "Hello World". Simpan panjangnya dalam n dengan len(...), dan salinan KAPAL dalam loud dengan .upper().
Click Run to see the output here. · Klik Jalankan untuk melihat output di sini.
name is "Mia" and age is 12. Use an f-string to print exactly: Mia is 12 years old. · name adalah "Mia" dan age adalah 12. Gunakan f-string untuk mencetak tepat: Mia is 12 years old.
Click Run to see the output here. · Klik Jalankan untuk melihat output di sini.
first is "ada" and last is "lovelace". Store the initials in initials as capital letters, each followed by a dot: "A.L.". · first adalah "ada" dan last adalah "lovelace". Simpan inisial dalam initials sebagai huruf kapital, masing-masing diikuti titik: "A.L.".
Click Run to see the output here. · Klik Jalankan untuk melihat output di sini.