Python3 Programming
#post_9### Python - Slicing Strings
#### Slicing
In Python, slicing allows you to extract a specific portion of a string by defining a range of indices. You can use the slice syntax, which involves specifying a start and end index, separated by a colon (:), to return part of the string.
##### Example:
To get the characters from position 2 to position 5 (excluding 5):
b = "Hello, World!"
print(b[2:5]) # Output: "llo"
Note: The first character has an index of 0.
---
#### Slice From the Start
If you leave out the start index, slicing will begin from the very first character in the string.
##### Example:
To get the characters from the start up to position 5 (excluding 5):
b = "Hello, World!"
print(b[:5]) # Output: "Hello"
---
#### Slice to the End
If you leave out the end index, the slicing will continue to the end of the string.
##### Example:
To get the characters from position 2 to the end:
b = "Hello, World!"
print(b[2:]) # Output: "llo, World!"
---
#### Negative Indexing
You can also use negative indexing to slice a string from the end. Negative indices count backward from the end of the string.
##### Example:
Get the characters from position -5 to -2 (excluding -2):
b = "Hello, World!"
print(b[-5:-2]) # Output: "orl"
---
#### Exercise:
What will be the result of the following code?
x = 'Welcome'
print(x[3:5])
Options:
- lcome
- come
- com
- co
---
### Python - Modify Strings
Python offers several built-in methods to modify strings. Let’s explore some of the most commonly used methods:
#### Upper Case
The upper() method converts all the characters in a string to uppercase.
##### Example:
a = "Hello, World!"
print(a.upper()) # Output: "HELLO, WORLD!"
---
#### Lower Case
The lower() method converts all the characters in a string to lowercase.
##### Example:
a = "Hello, World!"
print(a.lower()) # Output: "hello, world!"
---
#### Remove Whitespace
The strip() method removes any leading and trailing whitespace (spaces before and after the text).
##### Example:
a = " Hello, World! "
print(a.strip()) # Output: "Hello, World!"
---
#### Replace String
The replace() method replaces a part of the string with another string.
##### Example:
a = "Hello, World!"
print(a.replace("H", "J")) # Output: "Jello, World!"
---
#### Split String
The split() method splits a string into a list of substrings based on a specified separator (in this case, a comma).
##### Example:
a = "Hello, World!"
print(a.split(",")) # Output: ['Hello', ' World!']
---
### Python - String Concatenation
#### String Concatenation
To concatenate (or combine) two strings, you can use the
+
operator.
##### Example:
Merge variables a and b into variable c:
a = "Hello"
b = "World"
c = a + b
print(c) # Output: "HelloWorld"
##### Example:
To add a space between a and b, include a space in between:
a = "Hello"
b = "World"
c = a + " " + b
print(c) # Output: "Hello World"
---
#### Exercise:
What is the correct syntax to merge variables x and y into z?
- z = x, y
- z = x = y
- z = x + y
---
### النسخة العربية: Python - تقطيع النصوص
#### التقطيع
في بايثون، يمكنك استخدام التقطيع لاستخراج جزء محدد من النص عن طريق تحديد نطاق من الفهارس. يمكنك استخدام صيغة التقطيع، والتي تتضمن تحديد فهرس البداية وفهرس النهاية مفصولين بنقطتين (:) لإرجاع جزء من النص.
##### مثال:
لإرجاع الأحرف من الفهرس 2 إلى 5 (غير شامل):
b = "Hello, World!"
print(b[2:5]) # النتيجة: "llo"
ملحوظة: أول حرف في النص يحتوي على الفهرس 0.
---
#### التقطيع من البداية
إذا لم تحدد فهرس البداية**، فسيبدأ التقطيع من أول حرف في النص.
##### مثال:
لإرجاع الأحرف من البداية حتى الفهرس 5 (غير شامل):
b = "Hello, World!"
print(b[:5]) # النتيجة: "Hello"
---
#### التقطيع إلى النهاية
إذا لم تحدد
فهرس النهاية، فسيستمر التقطيع حتى نهاية النص.
##### مثال:
لإرجاع الأحرف من الفهرس 2 حتى النهاية:
b = "Hello, World!"
print(b[2:]) # النتيجة: "llo, World!"
---