البرمجة

تحويل النص إلى حالته الأصلية

To convert a string back to its original form after converting it to lowercase, you can use the upper() method in Python. Here’s how you can do it:

python
# Convert a string to lowercase original_string = "Hello, World!" lowercase_string = original_string.lower() print("Lowercase:", lowercase_string) # Convert the lowercase string back to the original string back_to_original = lowercase_string.upper() print("Back to original:", back_to_original)

In this example, the lower() method is used to convert the original string to lowercase, and then the upper() method is used to convert it back to the original form.

Keep in mind that this method will only work if the original string contains only letters that can be converted to lowercase and uppercase. If the original string contains characters that are not affected by case conversion, such as numbers or special characters, those characters will remain unchanged.

المزيد من المعلومات

للقيام بتحويل النص إلى الحالة الأصلية بعد تحويله إلى الحالة الصغيرة، يمكن استخدام الدالة upper() كما ذكرت سابقًا. ولكن إذا كان الهدف هو استعادة الحالة الأصلية للنص مع الحفاظ على الحروف الصغيرة التي كانت في النص الأصلي، فيمكن استخدام الدالة swapcase() بعد استخدام lower() لتحويل الحروف الكبيرة إلى حروف صغيرة ومن ثم استخدام swapcase() لعكس تبديل الحالات. إليك مثالًا على ذلك:

python
# Convert a string to lowercase original_string = "Hello, World!" lowercase_string = original_string.lower() print("Lowercase:", lowercase_string) # Convert the lowercase string back to the original string with maintaining the original case back_to_original = lowercase_string.swapcase() print("Back to original with maintaining case:", back_to_original)

في هذا المثال، يتم استخدام lower() لتحويل النص الأصلي إلى حالة صغيرة، ثم swapcase() لإعادة تبديل الحروف إلى الحالة الأصلية مع الحفاظ على الحالة الأصلية لكل حرف.

زر الذهاب إلى الأعلى