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

Ch 8 (1)

Uploaded by

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

Ch 8 (1)

Uploaded by

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

Function and string in

python
Ch 8
What is Function
Example of defining a function
def Add1(a,b):
C=a+b
Return(c)
Components of functions
• Name of Function Add1
• Arguments a,b
• Statements C=a+b
• Return value Return(c)
Built in function in python are:

• Print(“hello grade 8th”)


• Input(“enter the value of no”)
• Range(1,6)
User define Function
• Calling of Type1 Function
• Define Type1 Function
add1()
def add1():
a=int(input(“enter the first no”))
b=int(input(“enter the Second no”))
c=a+b
Print(‘sum of two nos are’,(c))
User define Function
• Calling of Type2 Function
• Define Type2 Function
add2(12,16)
def add2(a,b):
c=a+b
Print(‘sum of two nos are’,c)) a b
User define Function
• Define Type3 Function • Calling of Type3 Function
def add3(a,b): Result=add3(20,30)
c=a+b print(‘sum of two nos are’,Result)
Return(c)
How to Create a function
• Defining a function
def
Naming a function
def add1()
Supply parameters
def add1(a,b)
Body of function
c=a+b
Return(c)
How to call a function

Result=add3(20,30) // Calling a function


How to create string
• name=“goenka”
• Print(name)
• name is a string
How to create multiline string
Print(‘’’ String is a collection of alphabets, words or other characters. It
is one of the primitive data structures and are the building blocks for
data manipulation’’’)
It will print this string on screen
String is a collection of alphabets, words or other characters. It is one of
the primitive data structures and are the building blocks for data
manipulation
String operators +
First_name=“Sachin “
Last_name=“ Tendulkar”
Full_name= First_name+ Last_name
Print(full_name)
It will print this string on screen
SachinTendulkar
// + operator is called string concatenation operator. It will join the
strings
String operators *
String1=“India“
Name= String1 *3
Print(Full_name)
It will print this string on screen
IndiaIndiaIndia
// * operator is called string replication operator. It will duplicate the
strings
String Built in function
• Len
• Upper
• lower
• Capitalize
Len( ) Function
Length=len(“India“)
Print(length)
It will print this string on screen
5
// It will display total no of characters in string
upper( ) Function
Print(india.upper( )
It will print this string on screen
INDIA
// It will convert string in capital letters
lower( ) Function
Print(INDIA.lower( )
It will print this string on screen
india
// It will convert string in small letters
Captalize( ) Function
Print(INDIA. captalize( )
It will print this string on screen
India
// It will convert string first letter in capital letter

You might also like