Notes - PPS Unit 4
Notes - PPS Unit 4
https://www.studymedia.in/fe/notes
PPS Unit-VI DIT,Pimpri
Q 1. What is String? With the help of example explain how we can create string
variable in python.
Ans:
Strings data type is sequence of characters, where characters could be letter, digit,
whitespace or any other symbol.
a. Creation of Strings:
o Strings in Python can be created using single quotes or double quotes or even
triple quotes.
o Example:
string1 = 'Welcome' # Creating a String with single Quotes
string2 = "Welcome" # Creating a String with double Quotes
string3 = '''Welcome''' # Creating a String with Triple Quotes
b. Accessing strings:
o In Python, individual characters of a String can be accessed by using the method
of Indexing or range slice method [:].
o Indexing allows negative address references to access characters from the back
of the String, e.g. -1 refers to the last character, -2 refers to the second last
character and so on.
String W E L C O M E
Indexing 0 1 2 3 4 5 6
Negative Index -7 -6 -5 -4 -3 -2 -1
o Example:
string = 'Welcome'
print(string[0]) #Accessing string with index
print(string[1])
print(string[2])
print(srting[0:2]) #Accessing string with range slice
method
Output:
w
e
l
wel
Output:
String1 value is: Good
Address of String1 is: 1000
From the above output you can see string1 has address 1000 before modification. In
later output you can see that string1 has new address 3000 after modification.
It is very clear that, after some operations on a string new string get created and it has
new memory location. This is because strings are unchangeable/ immutable in nature.
Modifications are not allowed on string but new string can be created at new address
by adding/appending new string.
The % operator takes a format string on the left and the corresponding values in a
tuple on the right.
The format operator, % allows users to replace parts of string with the data stored in
variables.
The syntax for string formatting operation is:
"<format>" % (<values>)
The statement begins with a format string consisting of a sequence of characters and
conversion specification.
Following the format string is a % sign and then a set of values, one per conversion
specification, separated by commas and enclosed in parenthesis.
If there is single value then parenthesis is optional.
Following is the list of format characters used for printing different types of data:
Format Purpose
Symbol
%c Character
%d or %i Signed decimal integer
%s String
%o Octal integer
%x or %X Hexadecimal integer
%e or %E Exponential notation
Output:
Name = Amar and Age = 8
Name = Ajit and Age = 6
In the output, we can see that %s has been replaced by a string and %d has been replaced by
an integer value.
Indices in a String
Index from
P Y T H O N
the start
0 1 2 3 4 5
-6 -5 -4 -3 -2 -1
Index from
Syntax:
the end
string_name[start:end]
where start- beginning index of substring
end -1 is the index of last character
OUTPUT
str[1:5]= YTHO
str[ :6]= PYTHON
str[1: ]= YTHON
str[ : ]= PYTHON
10
str[-1]= N
str[ :-2 ]= PYTH
str[ -2: ]= ON
str[-5 :-2 ]= YTH
OUTPUT
str[ 2: 10]=lcome to
str[ 2: 10]= lcome to
str[ 2:10:2 ]=loet
str[ 2:10:4 ]=l
Whitespace characters are skipped as they are also part of the string.
4.6 ord() and chr() functions
Q 7.Write a short note on ord() and chr() functions
Ans.
The ord() function return the ASCII code of the character
11
For example:
str1=” Welcome to the world of Python!!!“ str1=” This is very good book“
str2=”the” str2=”best”
if str2 in str1: if str2 in str1:
print(“found”) print(“found”)
else: else:
print(“Not found”) print(“Not found”)
OUTPUT OUTPUT
Found Not found
You can also use in and not in operators to check whether a character is present in a
word.
For example:
‘u‘ in “starts” ‘v‘ not in “success”
12
OUTPUT OUTPUT
False True
These operators compare the strings by using ASCII value of the characters.
The ASCII values of A-Z are 65-90 and ASCII code for a-z is 97-122.
For example, book is greater than Book because the ASCII value of ‘b’ is 98 and ‘B’
is 66.
13
Example1:
first_str='Kunal works at Phoenix'
second_str='Kunal works at Phoenix'
print("First String:", first_str)
print("Second String:", second_str)
#comparing by ==
if first_str==second_str:
print("Both Strings are Same")
else:
print("Both Strings are Different")
Output:
First String: Kunal works at Phoenix
Second String: Kunal works at Phoenix
Both Strings are Same
14
Output:
First String: Kunal works at PHOENIX
Second String: Kunal works at Phoenix
Both Strings are Different
Using the !=(not equal to) operator for comparing two strings:
The != operator works exactly opposite to ==, that is it returns true is both
the strings are not equal.
Example:
first_str='Kunal works at Phoenix'
second_str='Kunal works at Phoenix'
print("First String:", first_str)
print("Second String:", second_str)
#comparing by !=
if first_str!=second_str:
print("Both Strings are Different")
else:
print("Both Strings are Same")
output:
First String: Kunal works at Phoenix
Second String: Kunal works at Phoenix
Both Strings are Same
15
print(“name2:”,name2)
print(“Both are same”,name1 is name2)
name2=”Kunal”
print(“name1:”,name1)
print(“name2:”,name2)
print(“Both are same”,name1 is name2)
Output:
name1=Kunal
name2=Shreya
Both are same False
name1=Kunal
name2=Kunal
Both are same True
In the above example, name2 gets the value of Kunal and subsequently
name1 and name2 refer to the same object.
4.9 Iterating strings
Q. No.10 How to iterate a string using:
Ans.
i) for loop with example
ii) while loop with example
Ans.
i) for loop:
for loop executes for every character in str.
The loop starts with the first character and automatically ends when
the last character is accessed.
Example-
16
str=”Welcome to python”
for i in str:
print(i,end=’ ’)
Output-
Welcome to Python
In the above program the loop traverses the string and displays each
letter.
The loop condition is index < len(message), so the moment index
becomes equal to the length of the string, the condition evaluates to
False, and the body of the loop is not executed.
Index of the last character is len(message)-1.
17
18