Program to get the length of the last word in a string using a function

In this python function program, we will get the length of the last word in a string using a function. Length means total number of characters/integers present in the word/number.

What is Function?
It is a block of code that executes when it is called.
To create a function use def keyword.

Steps to solve the program
  1. Create a function length.
  2. Use the def keyword to define the function.
  3. Take a string as input through the user.
  4. Convert the string into a list by splitting the string using split(” “).
  5. Get the length of the last word using the indexing and len() function.
  6. Print the output.
  7. Call the function to get the output.
				
					def length():
    str1 = input("Enter string: ")
    l = str1.split(" ")
    print(f"Length of the last word {l[-1]} in the string: ",len(l[-1]))
length()
				
			

Output :

				
					Enter string: sqatools in best for learning python
Length of the last word python in the string:  6
				
			

Related Articles

search words in a string.

get a valid mobile number.

Program to search words in a string using a function

In this python function program, we will search words in a string using a function.

What is Function?
It is a block of code that executes when it is called.
To create a function use def keyword.

Steps to solve the program
  1. Create a function search.
  2. Use the def keyword to define the function.
  3. Take a string and a word as input through the user.
  4. Create a count variable and assign its value equal to 0.
  5. Use a for loop to iterate over the words in the string after splitting it using split(” “).
  6. Use an if statement to check whether the word is in the string or not.
  7. If yes then add 1 to the count variable.
  8. Based on the value of the count variable and using an if-else statement determine whether the word is in the string or not.
  9. Print the respective output.
  10. Call the function to get the output.
				
					def search():
    str1 = input("Enter string: ")
    str2 = input("Enter word: ")
    count = 0
    for word in str1.split(" "):
        if word == str2:
            count += 1
    if count > 0:
        print(f"{str2} is in {str1}")
    else:
        print(f"{str2} is not in {str1}") 
search()
				
			

Output :

				
					Enter string: python programming
Enter word: python
python is in python programming
				
			

Related Articles

add two Binary numbers.

get the length of the last word in a string.

Program to add two Binary numbers using a function

In this python function program, we will add two Binary numbers using a function.

Binary Number: A number system where a number is represented by using only two digits (0 and 1) with a base 2 is called a binary number system.

Steps to solve the program
  1. Create a function binary.
  2. Use the def keyword to define the function.
  3. Pass two parameters i.e. binary numbers.
  4. Covert the binary numbers to the integers and then add them.
  5. Convert the addition to the binary number using bin().
  6. To remove the starting 0b from the number using indexing.
  7. Print the output.
  8. Pass the binary numbers to the function while calling the function.
				
					def binary(n1,n2):
    result = bin(int(n1,2)+int(n2,2))
    print(f"Addition of binary numbers {n1},{n2}: ",result[2:]) #to get rid of 0b
binary('100010','101001')
				
			

Output :

				
					Addition of binary numbers 100010,101001:  1001011
				
			

Related Articles

create a library management system.

search words in a string.

Program to create a library management system using a function

In this python function program, we will create a library management system using a function.

What is Function?
It is a block of code that executes when it is called.
To create a function use def keyword.

Steps to solve the program
  1. Create a function library.
  2. Use the def keyword to define the function.
  3. Pass two parameters i.e. book name and customers name.
  4. Print the book name and customer name.
  5. Pass the book name and customer name to the function while calling the function.
				
					def library(bname,cname):
    print("Book name: ",bname)
    print("Borrowed by :",cname)
library("Harry potter","Atharv")
				
			

Output :

				
					Book name:  Harry potter
Borrowed by : Atharv
				
			

Related Articles

program to reverse an integer.

add two Binary numbers.

Program to reverse an integer using a function

In this python function program, we will reverse an integer using a function.

What is Function?
It is a block of code that executes when it is called.
To create a function use def keyword.

Steps to solve the program
  1. Create a function reverse.
  2. Use the def keyword to define the function.
  3. Take a number as input through the user and create a variable rev and assign its value equal to 0.
  4. Use a while loop to find the reverse of a number.
  5. While num is greater than 0 divide the number by 10 using % to find the remainder.
  6. Multiply the rev by 10 and add the remainder to it.
  7. Now divide number by 10 using //.
  8. Print the final output.
  9. Call the function to see the output.
				
					def reverse():
    num=n=int(input("Enter a number: "))
    rev=0
    while n>0:
        r=n%10
        rev=(rev*10)+r
        n=n//10
    print("Reverse number: ",rev)
reverse()
				
			

Output :

				
					Enter a number: 225
Reverse number:  522
				
			

Related Articles

check whether the given year is a leap year.

create a library management system.

Program to check whether the given year is a leap year

In this python function program, we will create a function to check whether the given year is a leap year.


Leap Year: A year, occurring once every four years, which has 366 days including 29 February as an intercalary day. A Century year is a leap year only if it is perfectly divisible by 400.

What is Function?
It is a block of code that executes when it is called.
To create a function use def keyword.

Steps to solve the program
  1. Create a function leap.
  2. Use the def keyword to define the function.
  3. Pass a parameter i.e. year.
  4. Use the following condition to check whether the given year is a leap year or not – (year%400 == 0 or year%100 != 0) and year%4 == 0.
  5. Print the respective output.
  6. Pass the year to the function while calling the function.
				
					def leap(a):
    if (a%100!=0 or a%400==0) and a%4==0:
        print("The given year is leap year.")
    else:
        print("The given year is not leap year.")
leap(2005)
				
			

Output :

				
					The given year is not leap year.
				
			

Related Articles

create a Fruitshop Management system.

program to reverse an integer.

Python function program to create a Fruitshop Management system

In this python function program, we will create a Fruitshop Management system using function.

What is Function?
It is a block of code that executes when it is called.
To create a function use def keyword.

Steps to solve the program
  1. Create a function fruit.
  2. Use the def keyword to define the function.
  3. Pass three parameters i.e. fruit name, fruit price, and fruit quantity.
  4. Print the fruit name, fruit price, and fruit quantity.
  5. Calculate the total bill by multiplying fruit price and fruit quantity.
  6. Print the bill.
  7. Pass the fruit name, fruit price, and fruit quantity to the function while calling the function.
				
					def fruit(fname,fprice,quantity):
    print("Fruit name: ",fname)
    print("Fruit price: ",fprice)
    print("Fruit quantity in kg: ",quantity)
    print("Bill : ",fprice*quantity)
fruit("Apple",100,2)
				
			

Output :

				
					Fruit name:  Apple
Fruit price:  100
Fruit quantity in kg:  2
Bill :  200
				
			

Related Articles

get the addition with the return statement.

check whether the given year is a leap year.

Program to get the addition with the return statement

In this python function program, we will create a function to get the addition with the return statement.

What is Function?
It is a block of code that executes when it is called.
To create a function use def keyword.

Steps to solve the program
  1. Create a function add.
  2. Use the def keyword to define the function.
  3. Pass two parameter i.e. numbers.
  4. Get the addition of the numbers using ” + ” operator.
  5. Return the total.
  6. Pass the numbers to the function while calling the function.
				
					def add(a,b):
    total = a+b
    return total
add(5,15)
				
			

Output :

				
					20
				
			

Related Articles

print and accept login credentials.

create a Fruitshop Management system.

Python function program to print and accept login credentials

In this python function program, we will create a function to print and accept login credentials.

What is Function?
It is a block of code that executes when it is called.
To create a function use def keyword.

Steps to solve the program
  1. Create a function login.
  2. Use the def keyword to define the function.
  3. Take name and password as input through the function.
  4. Then print Credential accepted.
  5. Call the function to print the output.
				
					def login():
    name = input("Enter name: ")
    password = input("Enter password")
    print("Credential accepted")
login()
				
			

Output :

				
					Enter name: Atharv
Enter password1234
Credential accepted
				
			

Related Articles

get a list of odd numbers from 1 to 100.

get the addition with the return statement.

Python function program to get a list of odd numbers from 1 to 100

In this python function program, we will create a function to get a list of odd numbers from 1 to 100.

Odd number:
Odd numbers are those numbers that cannot be divided into two equal parts.

What is Function?
It is a block of code that executes when it is called.
To create a function use def keyword.

Steps to solve the program
  1. Create a function odd.
  2. Use the def keyword to define the function.
  3. Pass two parameter i.e. starting and ending numbers.
  4. Use for loop with the range function to iterate over values between 1-100.
  5. If a number is an odd number i.e. not divisible by 2 then print the number.
  6. Pass the starting and ending numbers to the function while calling the function.
				
					def odd(lower,upper):
    print("Odd numbers between", lower, "and", upper, "are:")

    for num in range(lower, upper + 1):
        if num%2 != 0:
            print(num,end=" ")
odd(1,100)
				
			

Output :

				
					Odd numbers between 1 and 100 are:
1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 
49 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91
93 95 97 99 
				
			

Related Articles

print a list of prime numbers from 1 to 100.

print and accept login credentials.