Q:

Python | Passing string value to the function

belongs to collection: Python String Programs

0

Example:

Input:
str = "Hello world"

Function call:
printMsg(str)

Output:
"Hello world"

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program:

# Python program to pass a string to the function

# function definition: it will accept 
# a string parameter and print it 
def printMsg(str):
	# printing the parameter 
	print str

# Main code 
# function calls 
printMsg("Hello world!")
printMsg("Hi! I am good.")

Output

Hello world!
Hi! I am good.

Write function that will accept a string and return total number of vowels

# function definition: it will accept 
# a string parameter and return number of vowels 
def countVowels(str):
	count = 0
	for ch in str:
		if ch in "aeiouAEIOU":
			count +=1 
	return count 

# Main code 
# function calls 
str = "Hello world!"
print "No. of vowels are {0} in "{1}"".format(countVowels(str),str)

str = "Hi, I am good."
print "No. of vowels are {0} in "{1}"".format(countVowels(str),str)

Output

No. of vowels are 3 in "Hello world!"
No. of vowels are 5 in "Hi, I am good."

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python String Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Python | Create multiple copies of a string by usi... >>
<< Python | Count vowels in a string...