Class 11 CBSE - Python
Python is designed to understand the user’s responsibility, the protocol, and the ethical issues related to the field of computing.
Python in class 11 for cbse is very handy when it comes to practice on computers but for the beginers there might be some errors and problems such as exception. But it's always advisable to practice program on computer than writing it on paper because it cherishes your practical skills , knowledge and typing speed and you'll come to know new things and if you want to be a software developer someday then its a must call for you.
Strings
Definition
In Python, a string is a sequence of characters enclosed or surrounded by single or double quotes. This
sequence of UNICODE characters may be a letter, a number, special characters, or a back slash
Python doesn’t have any char datatype like C++ or java does.
In Python, a string can be denoted by enclosing it within single quotation marks or double quotation marks.
Python treats both these quotes the same.
Example:
Creating string
The string can be created by simply enclosing characters in quotation marks whether, it can be single,
double or triple quotes.
Let us see the example how string is created:
In the above example,type function is used here to determine the object b value as string
Accessing String
We can easily access all the elements of the string one after another by using the subscript and index value
Each character in a string has an index value. Indexing starts at 0 and must be an integer
Indexing can be performed either from left or right side.Left indexing starts from 0 and right indexing
starts from -1.
Special String Operations
Concatenating String
The + operator is used to join or concatenate two strings, written on both sides of the operator, and
returns the new string after concatenating
Example:
You cannot concatenate a string object and an int object. To use the ‘+’ operator both the datatypes should
be the same
Replicating String
The * operator replicates multiple copies of the same string depending on the number of repetitions inputted
with * operator
Membership Operators
In python, we can also check whether a particular character exists in the given string or not using
membership operators. These operators are ‘in’ and ‘not in’
'in’ operator: This operator returns true or false depending on whether the character or substring exists in
a string
‘not in’ operator. This operator returns true when that specified character or substring does not exist in
the given string
String slicing
String slicing as the name suggests is used to return the substring of the string. This extracted substring
is termed a slice.
Here to extract the substring, we need to provide start index ,end index positions and the steps value which
are separated by a colon(:) in the slice operator
We can also use the negative index to slice the string from the end of the string. In negative indexing,
indexing starts from the right side
Basic String Operations
len()
This method is used to return the length of the string
Syntax:
len(str)
str refers to string here
Example:
capitalize()
This method is used to convert the first character to uppercase of the given string
Syntax:
str.capitalize()
str refers to string here
Example:
split()
This method is used to break the string on the basis of the specified separator and returns a list
of
substring
Here if the separator is not specified, so by default, it assumes space as a separator
Syntax:
str.split([seperator])
str refers to string here
Example:
replace()
This method is used to replace all the occurrences of the old string with the new string
Syntax:
str.replace(old,new)
str refers to string here
old refers to old string
new refers to new string
Example:
find()
This method is used to find the first occurrence of the substring in the given string ,and returns
its
lowest index of the substring ,if it is not found then it returns -1
Here first parameter is the substring which needs to be searched in the given string
The second parameter is the starting position where substring is to be searched in the given string
And the third parameter refers to the last value for the specified range where substring is to be
searched
Syntax:
str.find(sub,start,end)
str refers to string here
Example:
index()
This method is similar to find() method ,but it returns an error if that substring is not present in
the
given string
Syntax:
str.index(substring,start,end)
str refers to string here
Example:
isalpha()
This method is used to check whether the string contains only letters or not and returns a Boolean
value
i.e,true or false
Syntax:
str.isalpha()
str refers to string here
Example:
isalnum()
In this method, true is returned if all the characters are alphanumeric, which means alphabetic
letters
(a-z) and numbers (1-9)
Syntax:
str.isalnum()
str refers to string here
Example:
isdigit()
This method,returns true when the string contains only digits,otherwise false
Syntax:
str.isdigit()
str refers to string here
Example:
title()
This method is used to return the string with the first letter of every word in the string in
uppercase and
the rest in lowercase
Syntax:
str.title()
str refers to string here
Example:
count()
This method is used to return the number of times substring str occurs in the string.
If the start index and the end index are not specified the search starts from index 0 and ends with
the
length of the string
Syntax:
str.count(substring,start,end)
str refers to string here
Example:
lower()
This method is used to convert all the uppercase letters in the string into lowercase
Syntax:
str.lower()
str refers to string here
Example:
islower()
This method returns true if all the characters in the string are in lowercase
Syntax:
str.islower()
str refers to string here
Example:
upper()
This method is used to convert all the lowercase letters into uppercase letters in the string
Syntax:
str.upper()
str refers to string here
Example:
isupper()
This method returns true when all the characters in the string is in uppercase
Syntax:
str.isupper()
str refers to string here
Example:
lstrip()
This method returns string after removing extra space from the left side of the string
Syntax:
str.lstrip()
or
str.lstrip(chars)
str refers to string here
Example:
rstrip()
This method removes all the extra white spaces from the right of the string
Syntax:
str.rstrip()
or
str.rstrip(chars)
str refers to string here
Example:
strip()
This method returns string after removing extra spaces from both left and right side of the string
Syntax:
str.strip()
str refers to string here
Example:
isspace()
This method is used to check whether the string contains only whitespace characters or not
Syntax:
str.isspace()
str refers to string here
Example:
istitle()
This method is used to check whether the string is properly “titlecased” or not.It doesn’t take any
arguments
Syntax:
str.istitle()
str refers to string here
Example:
swapcase()
This method is used to convert all uppercase letters to lowercase and lowercase to uppercase of the
given
string
Syntax:
str.swapcase()
str refers to string here
Example:
partition(seperator)
This method is used to divide or split the string using specified separator and returns a tuple with
three
parts:
- Substring after the separator
- Separator itself
- Substring before the seperator
Syntax:
str.partition(seperator)
str refers to string here
Example:
This method is used to return the length of the string
Syntax:
len(str)
str refers to string here
Example:
len(str)
str refers to string here
This method is used to convert the first character to uppercase of the given string
Syntax:
str.capitalize()
str refers to string here
Example:
str.capitalize()
str refers to string here
This method is used to break the string on the basis of the specified separator and returns a list
of
substring
Here if the separator is not specified, so by default, it assumes space as a separator
Syntax:
str.split([seperator])
str refers to string here
Example:
str.split([seperator])
str refers to string here
This method is used to replace all the occurrences of the old string with the new string
Syntax:
str.replace(old,new)
str refers to string here
old refers to old string
new refers to new string
Example:
str.replace(old,new)
str refers to string here old refers to old string new refers to new string
This method is used to find the first occurrence of the substring in the given string ,and returns
its
lowest index of the substring ,if it is not found then it returns -1
Here first parameter is the substring which needs to be searched in the given string
The second parameter is the starting position where substring is to be searched in the given string
And the third parameter refers to the last value for the specified range where substring is to be
searched
Syntax:
str.find(sub,start,end)
str refers to string here
Example:
str.find(sub,start,end)
str refers to string here
This method is similar to find() method ,but it returns an error if that substring is not present in
the
given string
Syntax:
str.index(substring,start,end)
str refers to string here
Example:
str.index(substring,start,end)
str refers to string here
This method is used to check whether the string contains only letters or not and returns a Boolean
value
i.e,true or false
Syntax:
str.isalpha()
str refers to string here
Example:
str.isalpha()
str refers to string here
In this method, true is returned if all the characters are alphanumeric, which means alphabetic
letters
(a-z) and numbers (1-9)
Syntax:
str.isalnum()
str refers to string here
Example:
str.isalnum()
str refers to string here
This method,returns true when the string contains only digits,otherwise false
Syntax:
str.isdigit()
str refers to string here
Example:
str.isdigit()
str refers to string here
This method is used to return the string with the first letter of every word in the string in
uppercase and
the rest in lowercase
Syntax:
str.title()
str refers to string here
Example:
str.title()
str refers to string here
This method is used to return the number of times substring str occurs in the string.
If the start index and the end index are not specified the search starts from index 0 and ends with
the
length of the string
Syntax:
str.count(substring,start,end)
str refers to string here
Example:
str.count(substring,start,end)
str refers to string here
This method is used to convert all the uppercase letters in the string into lowercase
Syntax:
str.lower()
str refers to string here
Example:
str.lower()
str refers to string here
This method returns true if all the characters in the string are in lowercase
Syntax:
str.islower()
str refers to string here
Example:
str.islower()
str refers to string here
This method is used to convert all the lowercase letters into uppercase letters in the string
Syntax:
str.upper()
str refers to string here
Example:
str.upper()
str refers to string here
This method returns true when all the characters in the string is in uppercase
Syntax:
str.isupper()
str refers to string here
Example:
str.isupper()
str refers to string here
This method returns string after removing extra space from the left side of the string
Syntax:
str.lstrip()
or
str.lstrip(chars)
str refers to string here
Example:
str.lstrip() or str.lstrip(chars)
str refers to string here
This method removes all the extra white spaces from the right of the string
Syntax:
str.rstrip()
or
str.rstrip(chars)
str refers to string here
Example:
str.rstrip() or str.rstrip(chars)
str refers to string here
This method returns string after removing extra spaces from both left and right side of the string
Syntax:
str.strip()
str refers to string here
Example:
str.strip()
str refers to string here
This method is used to check whether the string contains only whitespace characters or not
Syntax:
str.isspace()
str refers to string here
Example:
str.isspace()
str refers to string here
This method is used to check whether the string is properly “titlecased” or not.It doesn’t take any
arguments
Syntax:
str.istitle()
str refers to string here
Example:
str.istitle()
str refers to string here
This method is used to convert all uppercase letters to lowercase and lowercase to uppercase of the
given
string
Syntax:
str.swapcase()
str refers to string here
Example:
str.swapcase()
str refers to string here
This method is used to divide or split the string using specified separator and returns a tuple with
three
parts:
- Substring after the separator
- Separator itself
- Substring before the seperator
Syntax:
str.partition(seperator)
str refers to string here
Example:
str.partition(seperator)
str refers to string here