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:
string Defination


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:

string Creation

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.

Accessing string


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:
Combining two strings

You cannot concatenate a string object and an int object. To use the ‘+’ operator both the datatypes should be the same
Error on combination of different datatypes


Replicating String
The * operator replicates multiple copies of the same string depending on the number of repetitions inputted with * operator

Replicating a string

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

Membership Operators in a 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

String slicing

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

string slicing from negative index


Basic String Operations





len()

This method is used to return the length of the string
Syntax:
        len(str)
        
str refers to string here

Example:

len function in a string



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:

capitalize function in a string



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:

split function in a string



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:

replace function in a string



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:

find function in a string



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:

index function in a string



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:

isalpha function in a string



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:

isalnum function in a string



isdigit()

This method,returns true when the string contains only digits,otherwise false
Syntax:
        str.isdigit()
        
str refers to string here

Example:

isdigit function in a string



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:

title function in a string



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:

count function in a string



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:

lower function in a string



islower()

This method returns true if all the characters in the string are in lowercase
Syntax:
        str.islower()
        
str refers to string here

Example:

islower function in a string



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:

upper function in a string



isupper()

This method returns true when all the characters in the string is in uppercase
Syntax:
        str.isupper()
        
str refers to string here

Example:

isupper function in a string



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:

lstrip function in a string



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:

rstrip function in a string



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:

strip function in a string



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:

isspace function in a string



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:

istitle function in a string



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:

swapcase function in a string



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:

swapcase function in a string



Contact Us

REACH US

SERVICES

  • CODING
  • ON-LINE PREPARATION
  • JAVA & PYTHON

ADDRESS

B-54, Krishna Bhawan, Parag Narain Road, Near Butler Palace Colony Lucknow
Contact:+ 919839520987
Email:info@alexsir.com