0% found this document useful (0 votes)
15 views

G12 - Python - Revision - Tour - II Part 1

hello

Uploaded by

ransh6339
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

G12 - Python - Revision - Tour - II Part 1

hello

Uploaded by

ransh6339
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Python Revision Tour-II

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.

• String can be created in following


ways-
1. By assigning value directly to the
String Literal
variable

2. User input Input ( ) always return input in


the form of a string.
Traversal of a
string
• Process to access each and every character of a string for the purpose of display or for some other purpose
is called string traversal.
• Every character in string is at different index position i.e. from 0 to size -1.

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: Output: Output:


Reverse of a String

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”

»* (it is used to replicate the string)


• like - 5*”@” will result into “@@@@@”
• Like - “go!” * 3 will result “go!go!go!”
note : - “5” * “6” expression is
invalid.
“abc” + 3 int + str =
invalid
Membership Operators

in and not in  checks the presence of character(s) in any string


**Note: Case-sensitive

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

word[ 0 : 14 ] will result into‘RESPONSIBILITY’


word[ 0 : 3] will result into‘RES’
word[ 2 : 5 ] will result into‘SPO’
word[ -7 : -3 ] will result into‘IBIL’
word[: 14 ] will result into‘RESPONSIBILITY’

word[: 5 ] will result into ‘RESPO’

word[ 3 : ] will result into ‘PONSIBILITY’


String
Functions
String.capitalize() Converts first character to Capital Letter
String.find() Returns the Lowest Index of Substring
String.index() Returns Index of Substring
String.isalnum() Checks Alphanumeric Character
String.isalpha() Checks if All Characters are Alphabets
String.isdigit() Checks Digit Characters
String.islower() Checks if all Alphabets in a String.are Lowercase
String.isupper() returns if all characters are uppercase characters
String.join() Returns a Concatenated String
String.lower() returns lowercased string
String.upper() returns uppercased string
len() Returns Length of an Object
ord() returns Unicode code point for Unicode character
reversed() returns reversed iterator of a sequence
slice() creates a slice object specified by range()
Let’s Try
Thank You
To be Continued ..

You might also like