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.
לאות יש מיקומים
- שרשורת היא שורה של תווים.
- לכל תווים יש מיקום הנקרא אינדקס.
- האינדקסים מתחילים ב
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)
- חלקיית שרשורת (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-strings בונים טקסט
- f-string מתחיל עם
fלפני התגובה. - שים ערך בתוך
{ }ופייתון תציב אותו בטקסט. - זו הדרך המסודרת לערבב מילים וערכים.
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". אחסן את האורך שלו בn באמצעות len(...), ועתק בגופן גדול (UPPERCASE) בloud באמצעות .upper().
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. · לחץ על הרץ כדי לראות את התוצא כאן.