Class 12: Passing string to the function

Example 1:
Python script to pass the student details to the function and display them.
Solution:

#function definition
def str_func1(n,r,s):
    print('name : ',n)
    print('roll : ',r)
    print('section : ',s)

#funtion calling
n=input('enter your name: ')
r=input('enter your roll no.: ')
s=input('enter your section : ')
str_func1(n,r,s)
#Output

enter your name: Aditi
enter your roll no.: 1
enter your section : A
name :  Aditi
roll :  1
section :  A
>>> 

Example 2:
Python script to pass the first name and last name to the function and display the full name.
Solution:

#function definition
def str_func1(fn,ln):
    print('My  name is : ',fn+' '+ln)
    
#funtion calling
n=input('enter your first name: ')
l=input('enter your surname.: ')
str_func1(n,l)
# Output

enter your first name: Aditya
enter your surname.: Sharma
My  name is :  Aditya Sharma
>>> 

Example 3:
Python script to pass a string to the function and display its length.
Solution:

#function definition
def str_func1(strn):
    l=len(strn)
    print('length of the string is :',l)
 
#funtion calling
n=input('enter any string: ')
str_func1(n)
# Output

enter any string: welcome to python
length of the string is : 17
>>> 

Example 4:
Python script to pass a string to the function and display it as a list of strings.
Solution:

#function definition
def str_func1(strn):
    l=list(strn)
    print('list created from string :',l)
    

#function calling
n=input('enter any string: ')
str_func1(n)
# Output

enter any string: welcome
list created from string : ['w', 'e', 'l', 'c', 'o', 'm', 'e']
>>> 

Example 5:
Python script to pass a string to the function and display it character by character.
Solution:

#function definition
def str_func1(strn):
    l=int(len(strn))
    for i in range(l):
        print(strn[i])    

#funtion calling
n=input('enter any string: ')
str_func1(n)
# Output:
enter any string: computer
c
o
m
p
u
t
e
r
>>>