ADEL AHMED ALHAJJ
Reference
ADEL AHMED ALHAJJ
String by Python
3
String by Python
As shown in Python, the slice
operator [] is used to access the
individual characters of the
string.
However, we can use the : (colon)
operator in Python to access the
substring from the given string.
Consider the following example.
4
String by Python
5
String by Python
6
String by Python
7
String by Python
We can do the negative slicing in the string; it starts from the rightmost
character, which is indicated as -1. The second rightmost index indicates -2,
and so on.
8
String by Python
9
String by Python
Searching Method >> Count() Method
It returns the number of occurences of substring in the specified range.
It takes three parameters, first is a substring, second a start index and third is last
index of the range. Start and end both are optional whereas substring is required.
10
String by Python
Searching Method >> find() Method
Python find() method finds substring in the whole string and returns index of
the first match. It returns -1 if substring does not match.
It takes three parameters, first is a substring, second a start index and
third is last index of the range. Start and end both are optional whereas
substring is required.
11
String by Python
12
String by Python
Manipulation Method >> lstrip() & rstrip() & strip() Method
13
String by Python >>> startswith() Method
Manipulation Method >> startswith() Method
Python startswith() method returns either True or False. It returns True if
the string starts with the A string which is to be checked., otherwise False.
It takes two parameters start and end. Start is a starting index from
where searching starts and end index is where searching stops
14
String by Python >>> startswith() Method
15
String by Python >>> >> endswith() Method
16
String by Python >>> isalpha() Method
Manipulation Method >> isalpha() Method
The isalpha() method returns True if all characters in a string are alphabetic
(both lowercase and uppercase) and returns False if at least one character is
not an alphabet. The whitespace, numerics, and symbols are considered as
non-alphabetical characters
17
String by Python >>> isalpha() Method
18
Control Structures(2)
Loops in Python
We use loops to make the same code execute a specified number of times,
either declared directly or determined based on the number of elements or
properties being processed by the loop.
Python provides a number of iteration methods (loops) that are used to
repeat parts of the program as many times as needed.
To determine the number of times a loop should repeat, all Python loops
check whether a certain expression evaluates to True or False, which tells
them whether to repeat one more time or to stop immediately.
19
Control Structures(2)
No Statement
1 For loop
2 while loop
Loop Control Statements
No Statement
1 Break Statement
2 Continue Statement
20
Control Structures(2) >> For Loop
Definition of the for Loop
We use the for loop to execute code a specified number of times. Through
it, we can easily iterate over all elements of a string or an array without
the need to define a counter and specify where it starts and where it ends.
for element in sequence:
statement1
statement2
21
Control Structures(2) >> For Loop
element is a normal variable that we define inside the loop. In each iteration,
a value from the sequence placed after it will be retrieved and stored in this
variable.
sequence is the string or array whose elements we want to access.
statements are all the commands placed inside the loop, and they are
executed in each iteration.
Here, the loop iterates over all elements of the array in order, from the first
element to the last element, and in each iteration, it stores the value of a
new element in the variable we defined.
for element in sequence:
statement1
statement2 22
Control Structures(2) >> For Loop
23
Control Structures(2) >> For Loop
24
Control Structures(2) >> For Loop
Here, the variable i will be sent to the string "hello", and each time it will
store a character inside it, and the character will be printed.
25
Control Structures(2) >> For Loop
Here, the variable i will be sent to the List, and each time it will store a
value from the List inside it, and the value will be printed.
26
Control Structures(2) >> For Loop
27
Control Structures(2) >> For Loop
28
Control Structures(2) >> For Loop
Here, the variable i will be sent to the Dictionary, and each time it will
store a Key from inside the Dictionary, and it will be printed.
29
Control Structures(2) >> For Loop
30
Control Structures(2) >> For Loop
31
Control Structures(2) >> For Loop
32
Control Structures(2) >> For Loop
33
Control Structures(2) >> For Loop
34
Python for Statement
35
Function Range( )
36
Function Range ( )
37
Function Range (Start , Stop)
38
Control Structures(2) >> For Loop with Brack
We can use Python break statement in for loop.
It is helpful to terminate the loop as soon as the condition is fulfilled instead
of doing the remaining iterations. It reduces execution time.
.
39
Control Structures(2) >> For Loop with Brack
40
Control Structures(2) >> For Loop with Brack
41
Control Structures(2) >> For Loop with Brack
42