سنناقش فى هذا الفصل النصوص فى بايثون | Python Strings، ستتعرف على نوع اليانات (النصوص) وكيفية استخدامها، مع بعض الأمثلة التوضيحية.


 

Python Strings

 

String عبارة عن سلسلة من الأحرف. في بايثون ، أي شيء بداخل علامات الاقتباس عبارة عن String.

 

الـ string محاط إما بعلامات اقتباس مفردة ( ‘ ‘ )  أو علامات اقتباس مزدوجة ( ” “ ). كالتالى:

print("Python") # Python
print('Python') # Python

إذا كان الـ string يحتوي على علامة اقتباس مفردة ، فيجب وضعها بين علامتي اقتباس مزدوجة والعكس صحيح، كالتالى:

print("That's Python")
print('"Python very Easy." - By Hamed Essam')

للتخطى من علامات الاقتباس ، يمكنك استخدام الشرطة المائلة (\). كالتالى:

print('That\'s Python')

 

تعيين نص لمتغير

 

يتم تعيين نص لمتغير باستخدام اسم المتغير متبوعًا بعلامة =  ثم النص، كالتالى:

message = "Python very Easy"
print(message)

Advertisements

 

Multiline String

 

يمكنك تعيين نصوص على أكثر من سطر لمتغير باستخدام ثلاث علامات اقتباس ( “””  “”” )، كالتالى:

# Using double quotes
a = """Mauris eu vulputate erat,
Donec ultricies eget est nec malesuada,
Sed viverra risus et mi rutrum rhoncus,
Nunc in vehicula quam. Nulla quis aliquet odio."""

# Using Single quotes
b = '''Mauris eu vulputate erat,
Donec ultricies eget est nec malesuada,
Sed viverra risus et mi rutrum rhoncus,
Nunc in vehicula quam. Nulla quis aliquet odio.'''
Mauris eu vulputate erat,
Donec ultricies eget est nec malesuada,
Sed viverra risus et mi rutrum rhoncus,
Nunc in vehicula quam. Nulla quis aliquet odio.
Mauris eu vulputate erat,
Donec ultricies eget est nec malesuada,
Sed viverra risus et mi rutrum rhoncus,
Nunc in vehicula quam. Nulla quis aliquet odio.

 

String with Arrays

 

مثل العديد من لغات البرمجة الشائعة الأخرى ، فإن النص عبارة هي صفائف من البايت تمثل أحرف unicode.

ليس لدى Python نوع بيانات حرف ، فالحرف الواحد هو ببساطة string بطول 1.

يمكن استخدام الأقواس المربعة [] للوصول إلى عناصر string، كالتالى:

x = "Python"
print(x[2]) # output = "t"

تذكر أن الحرف الأول لديه index (الموضع) 0.


 

تقطيع النصوص | Slicing

 

يمكنك إرجاع مجموعة من الأحرف باستخدام slice syntax، عن طريق تحديد index البداية والنهاية ، مفصولين بنقطتين ( : )، كالتالى:

x = "Python"
print(x[1:3]) # output = "yt"

 

Negative Indexing

 

استخدم index السالب لبدء الشريحة slice من نهاية string، كالتالى:

x = "Python is world"
print(x[-3:-1]) # output = "rl"

 

String Length

 

للحصول على طول الـ string، استخدم دالة ()len، كالتالى:

x = "Python is world"
print(len(x)) # output = 15

 

String Methods

 

تحتوي Python على مجموعة من الـ methods المضمنة التي يمكنك استخدامها للتعامل مع الـ string:

 

1. ()strip: تقوم بإزالة أي مسافة بيضاء white space من البداية أو النهاية، كالتالى:

x = "  Python is world!  "
print(x.strip()) # output = 'Python is world!'

2. ()lower: تقوم بتحويل جميع الأحرف إلى أحرف Small، كالتالى:

x = "PytTon Is WoRld!"
print(x.lower()) # output = python is world!

3. ()upper: تقوم بتحويل جميع الأحرف إلى أحرف Capital، كالتالى:

x = "python IS world!"
print(x.upper()) # output = PYTHON IS WORLD!

4. ()replace: تقوم بإستبدال النص بنص أخر، كالتالى:

x = "Python"
print(x.replace("P", "K")) # output = Kython

5. ()split: تقوم بتقسيم النص إلى نصوص فرعية إذا وجد مثيلات للفاصل، كالتالى:

x = "Python, world!"
print(x.split(",")) # output = ['Python', ' world!']

 

String Format

 

فى فصل Python Variables، لم نستطيع دمج String مع Number، بإستخدام هذه الطريقة:

y = 10
print("Number is " + y) # output = Error

ولكن يمكننا الجمع بين string و number باستخدام دالة الـ ()format. كالتالى:

week_num = 7
msg = "The number of days of the week {}" 
print(msg.format(week_num)) # output = The number of days of the week 7

تأخذ دالة الـ ()format الوسيطات التي تم تمريرها وتنسيقها ووضعها في string حيث تكون العناصر النائبة هى { }.

دالة الـ ()format تقبل أيضًا عددًا غير محدود من الـ parameters، ويتم وضعها في العناصر النائبة، كالتالى:

num_1 = 1
num_2 = 2
num_3 = 3

msg = "First number is {} and second number is {} and third number {}" 
print(msg.format(num_1, num_2, num_3)) # output = First number is 1 and second number is 2 and third number 3

يمكنك استخدام أرقام index للتأكد من وضع الـ parameters في العناصر النائبة الصحيحة، كالتالى:

num_1 = 1
num_2 = 2
num_3 = 3

msg = "First number is {1} and second number is {2} and third number {3}" 
print(msg.format(num_1, num_2, num_3)) # output = First number is 1 and second number is 2 and third number 3

 

Escape Character

 

لإدراج أحرف Illegal (غير قانونية) في النص، استخدم escape character.

escape character: هو شرطة مائلة \ يتبعها الحرف الذي تريد إدراجه.

من الـ escape characters المستخدمة في Python:

ResultCode
New Linen\
Carriage Returnr\
Tabt\
Backspaceb\
Form Feedf\
Octal valueooo\
Hex valuexhh\
Backslash\\
Single Quote‘\