String
String
String
Empty String : A string that does not contain any character is known as Empty String.
Example : (a) S = “” (b) S2 = ‘’
Multiline String : A string that span multiple lines is known as multiline String. It is a sequence of
characters enclosed in triple quotes”
Example : “””
This is a
Multiline string
“”
Indexing of the elements of a string :
With every element (character) of a string , an integer value is associated. This integer value is known as
index or subscript of that element. The index or subscript is used to access the individual element of the
string.
There are two type of indexing :
1. Positive indexing or forward indexing
2. Negative or Backward Indexing
Positive Indexing : In positive indexing , the index of the elements varies from 0 to N-1 where N is the
total number of characters in the string.
Negative Indexing : In negative indexing , the index of the elements varies from -N to -1 where N is the
total number of characters in the string.
Example : Let S = “COMPUTER” then it is stored as follow :
0 1 2 3 4 5 6 7
S C O M P U T E R
-8 -7 -6 -5 -4 -3 -2 -1
Example : S = “COMPUTER”
for ch in S :
print(ch)
0 1 2 3 4 5 6 7
S C O M P U T E R
-8 -7 -6 -5 -4 -3 -2 -1
OR
S = input(“Enter a string : “)
N = len(S)
for i in range(N) :
if S*i+ in ‘aeiouAEIOU’ :
V += 1
print(“Total number of vowels = “, V )
Q WAP to determine whether a given string is a palindrome or not. Example : ‘AMAMA’ is a palindrome.
Case 1 : N = 8
Mid = N/2 = 4
i j
0 1 2 3 4 5 6 7
S C O M P U T E R
-8 -7 -6 -5 -4 -3 -2 -1
J
Case 2 : N = 7
Mid = 7//2 = 3
i mid j
0 1 2 3 4 5 6
S C O M P U T E
-6 -6 -5 -4 -3 -2 -1
Ans:
s = input("Enter a string : ")
N = len(s)
mid = N//2
palin = True # assume that string is a palindrome
j = -1
for i in range(mid) :
if s[i] != s[j] :
palin = False # string is not a palindrome
break
j = j-1
if palin == True :
print("It is a palindrome")
else :
print("It is not a palindrome")
OR
if palin == True :
print("It is a palindrome")
else :
print("It is not a palindrome")
Example : S = “COMPUTER”
S*0+ = ‘P’ # ERROR
Q. Write a Program to find the frequency of different characters appeared in a given string.
2. Replicating Operator (*) : It creates a new string by repeating multiple copies the same string.
Syntax : <string_name>*<integer_value/variable>
Example :
S = “Amit”
S1 = S*3
print(S)
print(S1)
OUTPUT :
Amit
AmitAmitAmit
NOTE :
(i) The second operand for replicating operator (*) must be an integer value. It cannot be a
decimal value. If we write a decimal value then it will give an ERROR.
Example : S = “Amit”
S1 = S *3.3 # ERROR
(ii) If the second operand is an integer less than or equal to 0 then it will create an empty string.
Example : S = “Amit”
S1 = S *-2
print(“S1 = “, S1)
OUTPUT :
S1 = ‘ ‘
Q. Find the output :
s = "Amit"
Function : It is a named block of one or more program statements that can be executes as and when
require by calling/invoking the function.
Example :
def calarea():
R = float(input(“Enter Radius :”))
A = 3.14 *R*R
print(“Area of circle = “, A)
#-------------------------------------------------------------------------------------------
calarea() # calling statement
Arguments : The value/values that are passed to function at time of calling it are known as Arguments.
Example :
def calarea(R):
A = 3.14 *R*R
Print(“Area of circle = “, A)
#-------------------------------------------------------------------------------------------
Calarea(3.5) # calling statement
Type of Functions :
1. Built-in Function : Already defined in the python Interpreter ( Ex. float(), input(), print() )
2. User Defined Function : Created BY the Programmer
3. Pre-defined Library Functions ( Defined in python library)
Method : It is a block of one or more statements that acts upon the object.
It differs from the function in the way it is called /used in the program.
The calling statement for the method is as follow :
Object_name.Method_name()
Example : S = “amit”
S1 = S.capitalize()
print(“ S1 = “, s1 )
#-----------------------------------
S1 = "Computer"
s2 = "Amit"
L = length(s2)
print("Length of second string = ", L)
Syntax : len(<string-literal/String_object>)
Example : S = “COMPUTER”
N = len(S) # CALLING SATATEMENT
print(“:Length of string = “ , N)
2. capitalize() : It is a method that returns the exact copy of the string with the first letter in
uppercase form.
Syntax : <String_object>.capitalize()
Example : S= “abc”
S1= S.capitalize() # calling statement
print(“ Original string = “, S)
print(“Capitalize string = “, S1)
OUTPUT:
Original String = abc
Capitalize String = Abc
3. split() : It is a string method that breaks up a string at the specified separator(character) and
returns a list of substrings.
Where
Separator : It is a delimiter. The string splits at the specified separator. If the separator is not
specified then any whitespace ( space, newline etc ) is a separator.
Maxsplit : It defined the maximum number of splits. The default value of maxsplit is -1 which
means there is no limit on the number of splits.
Example :
Str1 = “ We are learning python”
X = str1.split()
print(X)
OUTPUT :
*‘We’, ‘are’, ‘learning’, ‘python’+
Example :
S = "abcd efgh ijkl mnop xyz"
S.split()
['abcd', 'efgh', 'ijkl', 'mnop', 'xyz']
S = "abcd dfcg gdhcgh hjchs"
S.split('c')
['ab', 'd df', 'g gdh', 'gh hj', 'hs']
S = "c dggdcgh fgchs"
S.split('c')
['', ' dggd', 'gh fg', 'hs']
S.split(sep = ' ', maxsplit = 3)
['abcd', 'efgh', 'ijkl', 'mnop xyz']
S.split(' ', 2)
['abcd', 'efgh', 'ijkl mnop xyz']
S.split(' ', 0)
['abcd efgh ijkl mnop xyz']
Q. WAP to convert first letter of each word in a string into capital letter and then display the string
Ans.
Str1 = input(“Enter a string : “)
Str2 = “” // empty string
List_words = str1.split()
3. Find() : It is a string method that is used to search the first occurrence a given substring in a
given string. It returns the lowest index of the substring if it is found in the string otherwise
return -1.
4. replace() : It is a string method that returns a string by replacing all the occurrences of the old
substring with the new substring.
Syntax : <string_object>.replace(<old sub_string>, <new sub_string>)
Example :
str1 = "Blue:Green:Orange:Red:Green"
str2 = "pink"
str3 = str1.replace("Green", str2)
print("Old String : ", str1)
print(" New string : ", str3)
OUTPUT :
Old String : Blue:Green:Orange:Red:Green
New string : Blue:pink:Orange:Red:pink
STRING SLICING :
Retrieving a substring from a given string is known as string slicing. Chunk of characters can be
extracted from a string using slicing operator (:) in square bracket[].
Slice operator return a slice of the string by returning characters falling between the starting position till
end-1. The default value of step is 1.
When step value is K, then it skips k-1 characters while slicing the string from the beginning.
When step value is -K, then it skips k-1 characters while slicing the string from the end.
Example :
0 1 2 3 4 5 6 7
S C O M P U T E R
-8 -7 -6 -5 -4 -3 -2 -1