python Function and string
python Function and string
b
Functions and Strings
Basic String
Types of operations
Arguments
String formatting
Calling operators
Function
Recursive Built-in
Functions & functions
Modules
Return
Statement
Dividing a large program into some small independent units or blocks known as functions.
When a function is called, the interpreter jumps to that function definition and executes
the statements in its body. Then, when the end of the body is reached, the interpreter
jumps back to the part of the program that called the function, and the program resumes
execution at that point. When this happens, we say that the function returns
Required arguments
The arguments are passed to a function in correct positional order. Also, the number of
arguments in the function call should exactly match with the number of arguments specified
in the function definition.
Otherwise
TypeError
is returned
def display(str,int_x,float_y):
print(‘The string is: ‘,str)
print(‘The integer value is: ‘,int_x)
Example:
print(‘The floating point value is:’,float_y)
display(float_y=56.04,str=‘Hello’,int_x=123)
Output:
The string is: Hello
The integer value is: 123
The floating point value is: 56.04
`
FUNCTIONS AND STRINGS 4/16/2025
Default Arguments:
Python allows, default values to specify function arguments.
If you do not know how many arguments that will be passed into your
function, add an asterisk (*) before the parameter name in the function
definition.
def my_function(*kids):
Output:
The youngest child is Sanjay
def interest(p,y,s):
if (s==y):
Output:
SI=float(p*y*12/100)
Enter the principle amount: 100000
return SI
Enter the number of years : 3
p=float(input(“Enter the principle amount:”))
Interest: 30000.0
y=float(input(“Enter the number of years:”))
print(“Interest:”,interest(p,y,s))
Output:
print(“Length:”,l,”\twidth:”,w,:\tHeight:”,h) volume: 48
print(‘volume:’,volume(4,6)) volume: 96
The Python lambda function is anonymous as it is a function without a def keyword and name.
Python lambda function doesn’t have any return statement. It has only a single expression which
arguments Expression
Identifier lambda keyword
Syntax:
def funtion_name():
statements
The user can only use the return
statement in a function. It cannot be return [expression]
used outside of the Python function.
A return statement includes the return
keyword and the value that will be
returned after that.
def adding(x, y):
i = x + y
return i
result = adding(16, 25)
print(f'Output of adding(16, 25) is {result}')
recursive solution
1 Problem divide into simpler sub-parts
def fact(n):
if(n==1 or n==0):
return 1
else:
return n*fact(n-1)
n= int(input('Enter the value of n:'))
print('The factorial of ',n,'is',fact(n))
Output:
Enter the value of n: 5
The factorial of 5 is 120
def gcd(x,y):
rem =x%y
if(rem==0): Output:
return y Enter the first number:50
else: Enter the second number:5
return gcd(y,rem) The GCD of given numbers is 5
n=int(input('Enter the first number:'))
m=int(input('Enter the second number:'))
print('The GCD of given numbers is',gcd(n,m))
String
String Variable The index of first character
is 0
Example:
Name = “Ravi” The index of last character
is n-1
Country = “India”
Nationality = str(“Indian”)
Where,
n = number of characters
★ Index
Individual characters in a string are accessed
using the subscript([]) operator
Output:
Message[0] = H
Message[1] = e
Message[2] = l
Message[3] = l
Message[4] = o
Str = ‘Hello’
Print(str*3)
Output:
HelloHelloHello
id ()string:
Every object in python is stored in memory. You can find the object
by using id(). The id() returns the memory address of that object
Str1 = ‘Hello’
print(str1)
print(‘id of str1 is:’,id(str1))
Str2 = ‘world’
print(str2)
Print(‘id of str2 is:’,id(str2))
Output:
Hello
Id of str1 is: 45093344
World
Id of str2 is: 45093346
%c character K
Slicing string:
A substring of a string is called a slice. [ sub-parts of sequence]
End: is last
character i.e n-1
The syntax: S[start : end]
Start specifies
the beginning of
index
P Y T H O N
Index
start 0 1 2 3 4 5
Index
-6 -5 -4 -3 -2 -1
end