يعتبر التعامل مع السلاسل النصية أمرًا شائعًا جدًا في لغة بايثون Python. وتدعم بايثون العديد من الطرق والوظائف للتعامل مع السلاسل النصية، بدءًا من تغيير حجم النص وتنسيقه وتحليله وتشفيره وفصله وإيجاد نص محدد في النص وغير ذلك الكثير. في هذا الدرس، قمنا بعرض استخدام بعض هذه الطرق والوظائف والتي تعد من أكثر الطرق استخدامًا. وقد تعلمنا كيفية استخدام هذه الوظائف والطرق وكيفية تطبيقها في برامج بايثون Python.
تتميز بايثون بوجود مجموعة واسعة من الوظائف والطرق المخصصة للتعامل مع السلاسل النصية، وتسمى هذه الوظائف “String Methods”. وتساعد هذه الوظائف على تحليل وتحويل السلاسل النصية بطريقة سهلة وفعالة.
وفيما يلي بعض الطرق والوظائف الأكثر استخدامًا للتعامل مع السلاسل النصية في Python:
تستخدم للحصول على عدد الأحرف في السلسلة النصية.
تستخدم لتحويل السلسلة النصية إلى الأحرف الكبيرة أو الصغيرة.
تستخدم لتقسيم السلسلة النصية إلى قطع منفصلة باستخدام فاصل محدد، ثم دمج هذه القطع في سلسلة نصية واحدة.
تستخدم لاستبدال جميع حالات ظهور النص القديم بنص جديد.
تستخدم لإزالة المسافات الزائدة أو الأحرف الزائدة من بداية أو نهاية السلسلة النصية.
تستخدم للبحث عن نص معين داخل السلسلة النصية وإرجاع موقع أول حالة ظهور له، أو الحصول على عدد مرات ظهور النص المحدد داخل السلسلة النصية.
تستخدم للتحقق مما إذا كانت السلسلة النصية تبدأ أو تنتهي بنص محدد.
وسوف نتناولها بالشرح والتوضيح بالأمثلة كما يلي:
بالطبع! سأذكر بعض الوظائف والطرق الشائعة للتعامل مع السلاسل النصية في Python مع أمثلة توضيحية لكل منها:
تستخدم للحصول على عدد الأحرف في السلسلة النصية.
string = "جمانة ربيع" length = len(string) print(length) # Output: 10
النتيجة :
10
تستخدم لتحويل السلسلة النصية إلى الأحرف الكبيرة أو الصغيرة.
string = "OMAR, RABEI!" uppercase_string = string.upper() lowercase_string = string.lower() print(uppercase_string) print(lowercase_string)
النتيجة:
OMAR, RABEI! omar, rabei!
تستخدم لتقسيم السلسلة النصية إلى قطع منفصلة باستخدام فاصل محدد، ثم دمج هذه القطع في سلسلة نصية واحدة.
string = "Hello, World!" split_string = string.split(", ") # returns ['Hello', 'World!'] joined_string = "-".join(split_string) # returns 'Hello-World!' print(split_string) print(joined_string)
النتيجة:
['Hello', 'World!'] Hello-World!
تستخدم لاستبدال جميع حالات ظهور النص القديم بنص جديد.
string = "Hello, World!" new_string = string.replace("World", "Python") print(new_string) # Output: Hello, Python!
النتيجة:
Hello, Python!
تستخدم لإزالة المسافات الزائدة أو الأحرف الزائدة من بداية أو نهاية السلسلة النصية.
string = " Gogo, Rabei! " stripped_string = string.strip() right_stripped_string = string.rstrip() left_stripped_string = string.lstrip() print(stripped_string) print(right_stripped_string) print(left_stripped_string)
النتيجة :
Gogo, Rabei! Gogo, Rabei! Gogo, Rabei!
تستخدم للبحث عن نص معين داخل السلسلة النصية وإرجاع موقع أول حالة ظهور له، أو الحصول على عدد مرات ظهور النص المحدد داخل السلسلة النصية.
string = "Hello, World!" position = string.find("World") count = string.count("l") print(position) # Output: 7 print(count) # Output: 3
النتيجة :
7 3
تستخدم للتحقق مما إذا كانت السلسلة النصية تبدأ أو تنتهي بنص معين.
string = "Hello, World!" starts_with_hello = string.startswith("Hello") ends_with_world = string.endswith("World!") print(starts_with_hello) # Output: True print(ends_with_world) # Output: True
النتيجة :
True True
تستخدم لجعل الحرف الأول من كل كلمة في السلسلة النصية كبيرًا.
string = "hello, world!" capitalized_string = string.capitalize() title_string = string.title() print(capitalized_string) # Output: "Hello, world!" print(title_string) # Output: "Hello, World!"
النتيجة :
Hello, world! Hello, World!
تستخدم للتحقق مما إذا كانت جميع الأحرف في السلسلة النصية حروفًا فقط أو أرقامًا فقط.
string_alpha = "hello" string_digit = "123" string_alpha_digit = "hello123" print(string_alpha.isalpha()) # Output: True print(string_digit.isdigit()) # Output: True print(string_alpha_digit.isalpha()) # Output: False
النتيجة :
True True False
\تستخدم للتحقق مما إذا كانت جميع الأحرف في السلسلة النصية كبيرة فقط أو صغيرة فقط.
string_upper = "OMAR" string_lower = "omar" string_mixed = "Omar" print(string_upper.isupper()) # Output: True print(string_lower.islower()) # Output: True print(string_mixed.isupper()) # Output: False
النتيجة :
True True False
تستخدم لتنسيق السلاسل النصية بشكل محدد باستخدام متغيرات أو قيم معينة.
name = "Omar" age =17 print("My name is {} and I'm {} years old.".format(name, age))
النتيجة :
My name is Omar and I'm 17 years old.
تستخدم للتحقق مما إذا كانت السلسلة النصية تحتوي فقط على حروف وأرقام.
string = "HelloWorld123" is_alphanumeric = string.isalnum() print(is_alphanumeric) # Output: True
النتيجة:
True
تستخدم للتحقق مما إذا كانت السلسلة النصية تحتوي فقط على حروف.
string = "HelloWorld" is_alpha = string.isalpha() print(is_alpha) # Output: True string = "123" is_alpha = string.isalpha() print(is_alpha) # Output: False
النتيجة :
True False
تستخدم للتحقق مما إذا كانت السلسلة النصية تحتوي فقط على أرقام عشرية.
string = "123.45" is_decimal = string.isdecimal() print(is_decimal) # Output: False
النتيجة :
False
تستخدم للتحقق مما إذا كانت السلسلة النصية تحتوي فقط على أرقام، بما في ذلك الأرقام العشرية والأرقام العربية والرومانية.
string1 = "1234" is_numeric1 = string1.isnumeric() print(is_numeric1) # Output: True string2 = "١٢٣٤" is_numeric2 = string2.isnumeric() print(is_numeric2) # Output: True string3 = "Ⅳ" is_numeric3 = string3.isnumeric() print(is_numeric3) # Output: True
النتيجة :
True True True
تستخدم للتحقق مما إذا كانت السلسلة النصية تمثل معرفًا في Python، وهي مجموعة من الأحرف والأرقام وشرطات التحتية (_).
string1 = "hello_world" is_identifier1 = string1.isidentifier() print(is_identifier1) # Output: True string2 = "123hello" is_identifier2 = string2.isidentifier() print(is_identifier2) # Output: False
النتيجة:
True False
تستخدم لإضافة أصفار في بداية السلسلة النصية حتى يصل طولها إلى الحد المحدد.
string = "Hello" new_string = string.zfill(10) print(new_string) # Output: '00000Hello'
النتيجة:
00000Hello
تستخدم لترجمة السلسلة النصية عن طريق إجراء تحويلات على الأحرف بناءً على قاموس محدد.
string = "Hello, World!" translation_table = string.maketrans("o", "e") new_string = string.translate(translation_table) print(new_string) # Output: 'Helle, Werld!'
النتيجة :
Helle, Werld!
مثال أخر:يمكن استخدام translate() لإزالة الأحرف غير المرغوب فيها من النص، على سبيل المثال:
text = "Hello, World! This is a test." # قائمة بالأحرف غير المرغوب فيها unwanted_chars = ["!", ",", ".", " "] # إنشاء القاموس translation_table = str.maketrans({char: None for char in unwanted_chars}) # استخدام الدالة لترجمة النص new_text = text.translate(translation_table) print(new_text) # Output: 'HelloWorldThisisatest'
النتيجة :
HelloWorldThisisatest
في هذا المثال، تم إنشاء قاموس باستخدام str.maketrans() والذي يحتوي على الأحرف غير المرغوب فيها، ثم تم استخدام الدالة translate() لإزالة هذه الأحرف من النص المحدد في المتغير text. النتيجة هي نص جديد يحتوي فقط على الأحرف المسموح بها.
الدالة rpartition() تقوم بتقسيم النص إلى ثلاثة أجزاء باستخدام الفاصل المحدد وترجعها على شكل tuple. الجزء الأول يحتوي على النص قبل الفاصل، الجزء الثاني يحتوي على الفاصل نفسه، والجزء الثالث يحتوي على النص بعد الفاصل.
وهنا مثال يوضح استخدام rpartition():
text = "I love Python programming language" last_space = text.rfind(" ") first_part, separator, last_part = text.rpartition(" ") print(first_part) # Output: "I love Python programming" print(separator) # Output: " " print(last_part) # Output: "language"
النتيجة :
I love Python programming language
في هذا المثال، تم استخدام rpartition() للعثور على أخر مسافة في النص باستخدام rfind()، ثم تم تقسيم النص إلى ثلاثة أجزاء باستخدام الفاصل المحدد وحفظها في العناصر first_part، separator، و last_part.
تقوم بإضافة مسافات (أو أي حرف آخر يتم تحديده) إلى اليسار من النص حتى يصل طول النص إلى الحجم المحدد. وتقوم بإرجاع النص الجديد.
وهنا مثال يوضح استخدام rjust():
text = "Hello" new_text = text.rjust(10) print(new_text) # Output: " Hello"
النتيجة:
Hello
في هذا المثال، يتم إنشاء نص “Hello”، ثم يتم استخدام rjust() مع الحجم المحدد 10. تم إضافة 5 مسافات إلى اليسار من “Hello” ليصل طول النص إلى 10. النتيجة النهائية هي ” Hello”.
الدالة rindex() تستخدم للعثور على موقع آخر حدوث لنص معين داخل سلسلة النصوص، ولكن يتم البحث من اليمين إلى اليسار بدلاً من اليسار إلى اليمين كما هو الحال في الدالة index().
وهنا مثال يوضح استخدام rindex():
text = "I love Python programming language and I love coding in Python" last_occurrence = text.rindex("Python") print(last_occurrence) # Output: 56
النتيجة:
56
في هذا المثال، تم استخدام rindex() للعثور على آخر حدوث للنص “Python” في النص الأصلي، ويتم البحث من اليمين إلى اليسار. تم العثور على “Python” في الموقع 46 في النص الأصلي، وتم إرجاع هذا الموقع. إذا لم يتم العثور على النص المحدد، فسترجع الدالة خطأ ValueError.
تعمل بطريقة مشابهة لدالة find() ولكنها تقوم بالبحث عن النص المعطى في السلسلة من اليمين إلى اليسار بدلاً من اليسار إلى اليمين. وترجع الموقع الأخير لظهور النص إن وجد، وإلا ترجع -1.
وهنا مثال يوضح استخدام rfind():
text = "I love Python programming language and I love coding in Python" last_occurrence = text.rfind("Python") print(last_occurrence) # Output: 46
النتيجة:
56
في هذا المثال، تم استخدام rfind() للعثور على آخر حدوث للنص “Python” في النص الأصلي، ويتم البحث من اليمين إلى اليسار. تم العثور على “Python” في الموقع 46 في النص الأصلي، وتم إرجاع هذا الموقع. إذا لم يتم العثور على النص المحدد، فسترجع الدالة -1.
تستخدم لإنشاء جدول ترجمة لاستخدامها مع الدالة translate()، حيث يمكن استخدام الجدول الناتج لتحويل الأحرف في سلسلة النصوص من نوع إلى آخر.
وهنا مثال يوضح استخدام maketrans():
intab = "aeiou" outtab = "12345" trantab = str.maketrans(intab, outtab) text = "this is a test" translated_text = text.translate(trantab) print(translated_text) # Output: th3s 3s 1 t2st
النتيجة:
th3s 3s 1 t2st
في هذا المثال، تم إنشاء جدول ترجمة باستخدام maketrans() حيث تم تحويل الحروف a، e، i، o، و u إلى الأرقام 1، 2، 3، 4، و 5 على التوالي. ثم تم استخدام الجدول الناتج مع دالة translate() لتحويل سلسلة النصوص الأصلية “this is a test” إلى “th3s 3s 1 t2st”.
تستخدم لإرجاع نسخة جديدة من السلسلة مع إضافة فراغات إلى اليمين لتحقيق الطول المحدد. تستخدم عادةً لتنسيق السلاسل للعرض بطريقة معينة، ويمكن تحديد العرض الإجمالي للسلسلة كمعامل في الدالة.
وهنا مثال يوضح استخدام ljust():
text = "Python" new_text = text.ljust(10) print(new_text) # Output: "Python "
النتيجة
Python
في هذا المثال، تم استخدام ljust() لإضافة فراغات إلى اليمين من سلسلة “Python” لجعلها بطول 10 أحرف. تم إنشاء نسخة جديدة من السلسلة تحتوي على الفراغات المضافة، وتم طباعتها.
تستخدم للتحقق مما إذا كانت جميع الأحرف في السلسلة “قابلة للطباعة” أم لا. يتم اعتبار الأحرف القابلة للطباعة هي تلك التي يمكن عرضها على الشاشة دون الحاجة إلى إجراءات خاصة، مثل الفراغات والحروف والأرقام والرموز العادية.
وهنا مثال يوضح استخدام isprintable():
text1 = "Hello, World!" text2 = "This is\na test" print(text1.isprintable()) # Output: True print(text2.isprintable()) # Output: False
النتيجة:
True False
في هذا المثال، تم استخدام isprintable() للتحقق مما إذا كانت السلاسل “Hello, World!” و”This is\na test” قابلة للطباعة أم لا. تمتلك السلسلة الأولى أحرف قابلة للطباعة فقط، بينما تحتوي السلسلة الثانية على حرف جديد السطر، مما يجعلها غير قابلة للطباعة، وتمت طباعة False.
تستخدم لتحويل الأحرف العلامة التبويب (tab) في السلسلة إلى فراغات (spaces). يمكن تحديد حجم الفراغ الذي يتم استخدامه بواسطة معامل اختياري، والذي يعتبر افتراضيًا هو 8 فراغات.
وهنا مثال يوضح استخدام expandtabs():
text = "Hello\tworld" new_text = text.expandtabs() print(new_text) # Output: "Hello world"
النتيجة:
Hello world
في هذا المثال، تم استخدام expandtabs() لتحويل الحرف التبويب بين الكلمتين “Hello” و “world” إلى ثمانية مسافات، حيث يعتبر الحجم الافتراضي للفراغ هو 8. تم إنشاء نسخة جديدة من السلسلة تحتوي على الفراغات المستخدمة بدلاً من الأحرف التبويب، وتم طباعتها.
تستخدم لتنسيق السلاسل النصية باستخدام قاموس كمدخل. يتم تعريف القاموس على أنه عبارة عن مجموعة من الأزواج القيمة-المفتاح التي يتم استخدامها لتحويل النص الأصلي.
وهنا مثال يوضح استخدام format_map():
text = 'My name is {name}, I am {age} years old and I am a {gender}' new_text = text.format_map(person) print(new_text) # Output: "My name is Ahmed, I am 25 years old and I am a male"
النتيجة:
My name is Ahmed, I am 25 years old and I am a male
في هذا المثال، تم استخدام format_map() لتنسيق السلسلة text باستخدام القاموس person، حيث تم استبدال العناصر الموجودة في الأقواس المعوَّضة بالقيم المحددة في القاموس. تم إنشاء نسخة جديدة من السلسلة تحتوي على النص الجديد المنسق بالشكل الصحيح، وتم طباعتها.
تستخدم لتحويل السلاسل النصية إلى صيغة “Casefolded”، والتي تعتبر صيغة مماثلة لصيغة “lowercase” لكنها أكثر شمولية وتتضمن أيضًا بعض الحالات الخاصة في اللغات الأخرى، مثل الأحرف المعدَّلة.
وهنا مثال يوضح استخدام casefold():
text = "HELLO, WORLD!" new_text = text.casefold() print(new_text) # Output: "hello, world!"
النتيجة:
hello, world!
في هذا المثال، تم استخدام casefold() لتحويل السلسلة text إلى صيغة “casefolded”، حيث تم تغيير الأحرف الكبيرة في السلسلة إلى أحرف صغيرة، وتم إنشاء نسخة جديدة من السلسلة تحتوي على النص الجديد المعدَّل، وتم طباعتها.
تستخدم لإدراج مسافات فارغة في بداية ونهاية السلسلة النصية لتوسيطها وجعلها محاطة بمسافات متساوية من الجانبين.
وهنا مثال يوضح استخدام center():
text = “Hello, World!”
new_text = text.center(20) print(new_text) # Output: " Hello, World! "
النتيجة:
Hello, World!
في هذا المثال، تم استخدام center() لإضافة مسافات فارغة إلى السلسلة text من كلا الجانبين، بحيث يكون طول السلسلة النهائية 20 حرفًا. وتم إنشاء نسخة جديدة من السلسلة تحتوي على النص المعدَّل، وتم طباعتها
تستخدم لتحويل سلسلة النصية إلى ترميز معين، مثل ترميز UTF-8 أو ASCII، والتي يمكن استخدامها لتخزين أو نقل البيانات.
وهنا مثال يوضح استخدام encode():
encoded_text = text.encode("UTF-8") print(encoded_text) # Output: b'Hello, World!'
النتيجة:
b'Hello, World!'
في هذا المثال، تم استخدام encode() لتحويل سلسلة النصية text إلى ترميز UTF-8. وتم إنشاء نسخة مشفرة من السلسلة الأصلية تم تخزينها في المتغير encoded_text، وتم طباعته. يلاحظ أن الحرف “b” قد أضيف إلى البداية من النص المشفر للدلالة على أنه عبارة عن مصفوفة بايت.
Your upper and decrease legs ought to be at a right angle
at the backside. Keep a impartial spine and head position and look straight ahead in the course of the
reducing motion. Maintain the machine’s handles in a comfortable place with a neutral grip.
Turn out your toes barely and guarantee your knees align with your toes.
The landmine sumo deadlift is an superior different to the hack squat.
It retains the torso upright with important knee flexion, supplying you
with the hack squat’s advantages of elevated quad activation and decreased decrease back stress.
As mentioned, hack squat machines include a sled that runs
up and down a monitor on an angle. At the
bottom of the sled is a platform you stand on so your back is on the sled.
Typically, an altered machine that simulates
a squat’s motion while supporting the neck and back is used for doing the hack squat.
Some athletes could choose to have their feet excessive on the
foot plate while others choose them lower. Research have concluded
that foot placement on this exercise isn’t that important.
Muscle activation was comparable whether lifters used a wide or narrow stance.
(3) The same principle may be utilized to the hack squat
as properly, but this could be determined by the person training.
The key with a hack squat is to get your knees to a 90-degree angle.
Negative suggestions is limited, with a couple feedback in regards to
the padding high quality and the lack of written directions within the user handbook.
They’re constructed to deal with severe exercises, but
they’re designed to fit into your dwelling area with
out taking up the whole room. You don’t see these as a lot as you
used to, however I actually like them. The vertical leg press machine is an excellent alternative for people who have restricted house and are trying to
get some exercise. The leverage leg press machine’s elements
work collectively to form a cohesive entire. What we like about it
is how it’s solidly constructed, and moving into the machine and doing lower physique exercises almost seems like an afterthought.
It’s well-designed and intuitive, which implies
you won’t take more than a minute to determine issues out.
We’re dedicated to serving as your most reliable athlete useful resource, on your sport and
fitness journey. We try to educate and inform our guests so that they will make
the right training, nutrition and supplement choices.
Our aim is to provide factual information and unbiased product suggestions for our readers
based mostly on related expertise, in depth analysis and feedback.
He has run half marathons, accomplished mud runs, positioned in body transformation contests, coached wrestling, and now coaches girls’ soccer.
Not to say he has also tried literally tons of of supplements through the years and has
an unlimited and thorough supplement data. He has
written for Muscle & Energy, Testosterone Junkie, The Game Evaluate and other publications.
With proper approach and variations, it can cater to totally different health ranges and targets.
The hack squat machine is a bit of essential fitness center equipment used for doing leg workout routines.
If you have this machine in your fitness center, try
to do the hack squat loading lbs weight plates on all sides.
In The End, incorporating each exercises into your exercise routine can present a well-rounded method to energy coaching.
Rather than choose barbell hack squats or front squats, think about combining each
for a quad workout that’s second to none.
Make positive you arrange safety bars when utilizing
a squat rack or energy rack so that you simply can always place the barbell
down safely should you get stuck. When squatting,
be certain to stay inside the safety bars of the
rack. A place to debate the crushing of carbitalism and democratic ownership over the
means of muscle manufacturing. Using the leg press is
a simple and intuitive course of that ought to be straightforward once
you get use to it. You additionally wish to use a product that has the right size pegs
(Olympic at 2″ or ‘standard’ at 1″) or
at least an adapter peg. You have to ensure that this product suits the weights
you’ve at residence, since they do not come with weight.
On high of this, you’re most likely going to want to lubricate this product’s rails since they tend to squeak during early usage.
The Smith machine is a versatile piece of kit that mixes elements of
a barbell and a machine. It offers a guided motion along
a onerous and fast bar, aiding in stability and safety. This maximizes the advantages of the product and really does
minimize down the relative value of the product since you can carry out a quantity of exercises,
effectively, with a single machine. On high of this, the standard and buyer satisfaction are reliably great, and the
general product seems to be a great alternative.
Performing the machine hack squat is kind
of simple, and most lifters find it relatively snug.
While the train could be challenging when first trying it, the movement is relatively simple.
A ultimate hack squat variation is to easily reverse your
place in the common hack squat machine, which means that you just face the
machine instead of leaning your back against it.
References:
steroids without side effects (src.Eruta.nl)
70918248
References:
supplements with steroids