G12 - Python - Revision - Tour - II Part 1
G12 - Python - Revision - Tour - II Part 1
Grade-12
CBSE
Session 1 :
Strings in Python
String
Characteristics
• A sequence of characters enclosed in single, double or triple quotes.
• Strings are immutable; i.e.., they cannot be changed.
Forwarding
Back warding
Example 1: Example 2: Example 3:
str=“python” str=“python” str=“python”
for i in range(0,len(str)): for i in range(0,len(str)): for i in range(0,len(str)):
print(str[i]) print(str[i],end=‘’) print(str[i],end=“#”)
Output
String Operators
• There are 2 operator that can be used to work upon strings +
and *.
»+ (it is used to join two strings)
• Like - “tea” + “pot” will result into “teapot”
• Like- “1” + “2” will result into “12”
• Like – “123” + “abc” will result into “123abc”
Example Output
“a” in “python” False
“a” in java True
“per” in “operators” True
“Man” in “manipulation” False
“Man” not in “manipulation” True
Comparison Operators
Str1=“python”
Str2=“program” Example Output
Str3=“Python” Str1 == Str2 False
Str1 != Str2 True
Str1 == Str3 False
Str1 > Str2 True
Str3 > Str1 False
Str2 > Str3 True
Note: Comparison depends only on the ASCII value not on the length of the string
Built in function i) ord() is used to display the ASCII of characters.
ii) chr() functions return the character of the given ASCII value.
String Slicing
Forwarding 0 1 2 3 4 5 6 7 8 9 10 11 12 13
Word R E S P O N S I B I L I T Y
Backward -14 -13 -12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1
Syntax
str [start: stop: step]
String Slicing
• Look at following examples carefully-
Forwarding 0 1 2 3 4 5 6 7 8 9 10 11 12 13
Word R E S P O N S I B I L I T Y
Backward -14 -13 -12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1