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:].
أخذ جزء من السلسلة
- الجزء المستخرج يأخذ جزءاً من السلسلة:
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.
strings f تبني النصوص
- الـ 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". خزن طولها في n باستخدام len(...)، ونسخة بأحرف كبيرة في 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. · اضغط تشغيل لرؤية المخرجات هنا.