الدالة print()
تستخدم لعرض النصوص أو النتائج على الشاشة.
print("مرحبا بك في عالم Python!")
الدالة input()
تستخدم لأخذ مدخلات من المستخدم.
name = input("أدخل اسمك: ")
print("أهلاً", name)
النصوص (Strings) في بايثون توفر العديد من الدوال المفيدة لمعالجة وتعديل النصوص. فيما يلي شرح لأهم الدوال مع أمثلة ونتائجها:
# تعريف نص مع مسافات في البداية والنهاية
text = " Hello Python 3 "
print("original:", repr(text)) # عرض النص الأصلي مع علامات الاقتباس
print("1. upper():", text.upper()) # تحويل كل الأحرف إلى أحرف كبيرة
print("2. lower():", text.lower()) # تحويل كل الأحرف إلى أحرف صغيرة
print("3. capitalize():", text.capitalize()) # أول حرف كبير والباقي صغير
print("4. title():", text.title()) # أول حرف من كل كلمة كبير
print("5. swapcase():", text.swapcase()) # عكس حالة الأحرف
print("6. strip():", text.strip()) # إزالة المسافات من البداية والنهاية
print("7. lstrip():", text.lstrip()) # إزالة المسافات من اليسار فقط
print("8. rstrip():", text.rstrip()) # إزالة المسافات من اليمين فقط
print("9. replace('Python', 'World'):", text.replace("Python", "World")) # استبدال كلمة بكلمة أخرى
print("10. count('l'):", text.count('l')) # عدد مرات ظهور الحرف l
print("11. find('Python'):", text.find('Python')) # إيجاد موقع كلمة Python
print("12. rfind('o'):", text.rfind('o')) # إيجاد آخر موقع لحرف o
print("13. startswith(' He'):", text.startswith(' He')) # هل يبدأ النص بـ ' He'
print("14. endswith('3 '):", text.endswith('3 ')) # هل ينتهي النص بـ '3 '
print("15. isalpha():", text.isalpha()) # هل كل النص أحرف فقط
print("16. isdigit():", text.isdigit()) # هل كل النص أرقام فقط
print("17. isalnum():", text.isalnum()) # هل النص أحرف أو أرقام فقط
print("18. isspace():", text.isspace()) # هل النص مسافات فقط
print("19. split():", text.split()) # تقسيم النص إلى قائمة كلمات
print("20. join(['a', 'b', 'c']):", "-".join(['a', 'b', 'c'])) # دمج قائمة نصوص بفاصل -
print("21. zfill(20):", text.zfill(20)) # إضافة أصفار من اليسار حتى يصل الطول إلى 20
print("22. center(30):", text.center(30, '*')) # توسيط النص داخل 30 خانة مع ملء الفراغ بـ *
print("23. encode():", text.encode()) # تحويل النص إلى بايتات
print("24. casefold():", text.casefold()) # تحويل النص إلى أحرف صغيرة بشكل قوي
original: ' Hello Python 3 '
1. upper(): HELLO PYTHON 3
2. lower(): hello python 3
3. capitalize(): hello python 3
4. title(): Hello Python 3
5. swapcase(): hELLO pYTHON 3
6. strip(): Hello Python 3
7. lstrip(): Hello Python 3
8. rstrip(): Hello Python 3
9. replace('Python', 'World'): Hello World 3
10. count('l'): 2
11. find('Python'): 8
12. rfind('o'): 12
13. startswith(' He'): True
14. endswith('3 '): True
15. isalpha(): False
16. isdigit(): False
17. isalnum(): False
18. isspace(): False
19. split(): ['Hello', 'Python', '3']
20. join(['a', 'b', 'c']): a-b-c
21. zfill(20): 000 Hello Python 3
22. center(30): **** Hello Python 3 ****
23. encode(): b' Hello Python 3 '
24. casefold(): hello python 3
ملاحظات:
بعد ما تتعلم التعامل مع النصوص، يمكنك الانتقال إلى درس المتغيرات.