تعلم التعامل مع النصوص (Strings) في بايثون خطوة بخطوة

مرحباً بك في عالم النصوص في بايثون! تخيل أنك تريد كتابة رسالة ترحيب، أو تخزين اسم مستخدم، أو التعامل مع أي جملة في برنامجك. هذا هو بالضبط دور الـ Strings، أو "النصوص". في هذا الدرس، سنتعلم معاً كيفية إنشاء النصوص والتعامل معها بكل سهولة، وهي من الأساسيات المهمة جداً لأي مبرمج.

ما هي الـ Strings (النصوص)؟

في لغة بايثون، الـ String هو نوع بيانات يستخدم لتمثيل النصوص. يمكن أن يكون حرفاً واحداً، كلمة، جملة كاملة، أو حتى فقرة طويلة. ببساطة، أي شيء تكبعه بين علامتي اقتباس (' ' أو " ") يعتبر نصاً في بايثون.

مثال بسيط:

# These are all examples of strings
greeting = "Hello World"  # Text inside double quotes
name = 'Ahmed'                # Text inside single quotes
single_char = "A"             # Even a single character is considered a string
empty_text = ""               # This is also a string, but it is an empty string

طرق إنشاء النصوص: علامات الاقتباس

تستخدم بايثون نوعين رئيسيين من علامات الاقتباس لإنشاء النصوص، وكلاهما يؤدي نفس الغرض:

  1. علامات الاقتباس المفردة: 'نص'
  2. علامات الاقتباس المزدوجة: "نص"

لماذا يوجد نوعان؟ السبب بسيط ومفيد! إذا كان نصك يحتوي على علامة اقتباس واحدة بداخله، يمكنك استخدام النوع الآخر لتحديد النص.

مثال توضيحي:

# The text contains a single quote, so we use double quotes to define it.
message1 = "It's a beautiful day!"

# The text contains double quotes, so we use single quotes to define it.
message2 = 'He said, "Hello!" and smiled.'

print(message1)
print(message2)

النصوص متعددة الأسطر (Multiline Strings)

ماذا لو أردت كتابة نص طويل يغطي أكثر من سطر؟ هنا يأتي دور علامات الاقتباس الثلاثية (''' ''' أو """ """). تسمح لك بكتابة نص يمتد على عدة أسطر كما هو.

مثال:

# Create a multi-line string using triple quotes
long_message = """Hello Ahmed,

Thank you for joining Codex Academy.
We wish you an enjoyable and useful learning journey.

Best regards, the team.
"""

print(long_message)

دمج النصوص (String Concatenation)

"الدَمْج" يعني ربط نصين أو أكثر معاً لإنشاء نص جديد. نستخدم لهذا الغرض علامة +.

مثال:

first_name = "Ali"
last_name = "Khalid"

# Concatenate strings using the + operator
full_name = first_name + " " + last_name
print(full_name)  # Will print: Ali Khalid

# Another example: creating a greeting sentence
greeting = "Welcome, " + first_name + "!"
print(greeting)   # Will print: Welcome, Ali!

ملاحظة مهمة: لا يمكنك دمج نص مع رقم باستخدام + مباشرة. سنتعلم كيفية تحويل الأرقام إلى نصوص في دروس لاحقة.

الوصول إلى الأحرف داخل النص (Indexing)

يمكنك التفكير في النص كسلسلة من الأحرف مرتبة بجانب بعضها. لكل حرف رقم فهرس (Index) يحدد موقعه. المهم أن تعرف أن الترقيم يبدأ من الصفر (0) في البرمجة، وليس من الواحد.

مثال:

word = "Python"
# index:       0   1   2   3   4   5
# character:   P   y   t   h   o   n

print(word[0])  # Will print: P
print(word[1])  # Will print: y
print(word[5])  # Will print: n

استخراج جزء من النص (Slicing)

إذا كان الـ Indexing يعطيك حرفاً واحداً، فإن الـ Slicing يسمح لك باستخراج Set متتالية من الأحرف (جزء من النص). نستخدم النقطتين : للقيام بذلك.

الصيغة: [start:end]

  • start: الفهرس الذي تبدأ منه القطعة (شامل).
  • end: الفهرس الذي تنتهي عنده القطعة (غير شامل)، أي أن الحرف في هذا الفهرس لا يدخل في النتيجة.

مثال:

language = "JavaScript"

# Extract characters from index 0 to index 4 (excluding 4)
slice1 = language[0:4]
print(slice1)  # Will print: Java

# If the first index is omitted, it starts from the beginning (0)
slice2 = language[:4]
print(slice2)  # Will also print: Java

# If the last index is omitted, it ends at the end of the string
slice3 = language[4:]
print(slice3)  # Will print: Script

# Extract the entire string
slice4 = language[:]
print(slice4)  # Will print: JavaScript

ماهو طول النص؟ دالة len()

لمعرفة عدد الأحرف الموجودة داخل نص معين (بما في ذلك المسافات وعلامات الترقيم)، نستخدم الدالة المدمجة len().

مثال:

my_string = "I love learning Python!"
length = len(my_string)

print("The text is:", my_string)
print("The number of characters in this text is:", length)  # Will print: 24

دعنا نتدرب معاً!

لنقم بإنشاء برنامج بسيط يطبق ما تعلمناه:

# 1. Create strings
title = "Programming Adventure"
chapter = 'Chapter One: The Beginning'

# 2. Concatenate strings
story_start = title + " - " + chapter
print("Today's story:", story_start)

# 3. Access characters
first_letter = title[0]
print("The first letter of the title is:", first_letter)

# 4. Extract part of the text
keyword = title[0:4]
print("Keyword:", keyword)

# 5. Get length
text_length = len(title)
print("Title length:", text_length, "characters")