6 - Strings
6 - Strings
www.tvtc.gov.sa
Strings
Pearson Education , Edited by :Nouf Almunyif
1
10/6/2021
Multiline Strings
• You can assign a multiline string to a variable by using three quotes Or
three single quotes:
2
10/6/2021
3
10/6/2021
Accessing the
Individual
Characters in
a String
4
10/6/2021
5
10/6/2021
B o s t o n
0 1 2 3 4 5
B o s t o n
0 1 2 3 4 5
6
10/6/2021
String Concatenation
• Concatenation: appending one string to the end of another string
• Use the + operator to produce a string that is a combination of its operands
• The augmented assignment operator += can also be used to concatenate
strings
• The operand on the left side of the += operator must be an existing variable; otherwise, an
exception is raised
7
10/6/2021
8
10/6/2021
String Slicing
9
10/6/2021
String Slicing
ABCDEFGHIJKLMNOPQRSTUVWXYZ
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
letters = ‘ABCDEFGHIJKLMNOPQRSTUVWXYZ’
print letters[0:26:2] # ‘ACEGIKMOQSUWY’
10
10/6/2021
11
10/6/2021
String Methods
12
10/6/2021
String Methods
•Strings in Python have many types of methods, divided into different types of
operations
•General format:
mystring.method(arguments)
•Some methods test a string for specific characteristics
•Generally Boolean methods, that return True if a condition exists, and False
otherwise
String
Methods
Boolean
13
10/6/2021
String Methods
•Some methods return a copy of the string, to which modifications have been made
•Simulate strings as mutable objects
•String comparisons are case-sensitive
•Uppercase characters are distinguished from lowercase characters
•lower and upper methods can be used for making case-insensitive string
comparisons
String Methods
14
10/6/2021
String Methods
String Methods
15
10/6/2021
Splitting a String
•split method: returns a list containing the words in the string
• By default, uses space as separator
• Can specify a different separator by passing it as an argument to the split
method
16
10/6/2021
Splitting a String
Splitting a String
17
10/6/2021
format() Read
format() Read
18
10/6/2021
format() Read
العنوان الفرعي
19