Python Program to Find Length of a String Q. Write a Python program to take user input and calculate length of a string. Use built in len() Python function to calculate the length of a string. This function take one argument of type object. If the input object is a string, this function will calculate length of a string. Example:

| #Python program to calculate length of a string x = "Tecadmin" n = len(x) print(n) |

Output 8 Example with User Input Another Python program to take input of string from user and calculate the length.

| #Python program to take user input and calculate string length x = input("Enter a string: ") n = len(x) print(n) |

Execute above Python script. enter a string when…

Read More Python Program to Generate A Random Number Q. Write a Python program to generate a random number. A random number can be any number between given two numbers. Use randint() function defined under the random python module. It accept two parameters of start and end number.

| ## Python program to generate a random number import random x = random.randint(10, 50) print(x) |

Execute above script and it will return a random number between given numbers. Output: 33 Output value may change with each run.

Read More Python Program to Find Factorial of a Number Q. Write a Python program to take input of a number and find the factorial of that number. Example: A sample Python program to calculate factorial of a given number. You can also take input of a number from user and calculate the factorial.

| # Initialize a number num = 5 # Uncomment below to take input from user #num = int(input("Enter a number: ")) fact = 1 if num < 0: print("Sorry, factorial does not exist for a negative number") elif num == 0: print("The factorial of 0 is 1") else: for i in range(1, num + 1): fact = fact*i print("The factorial of", num, "is", fact) |

Output: The factorial of 5 is 120

Read More Python Program to Print All Prime Numbers Q. Write a Python program to print all prime numbers between 1 and given number. Also write program to take input of two numbers and print prime numbers between them. In our previous tutorial, you have learned to check if a number is prime number. You just need to use code inside a for loop. Example: A sample Python program to take input of maximum number and print all available prime numbers between 1 and given number.

| max = int(input("Enter maximum number: ")) for num in range(1,max + 1): if num > 1: for i in range(2, num): if (num % i) == 0: break else: print(num) |

Ouput: Enter max number: 20…

Read More Check If Two Strings Are Equal using Python Use == operator to test if two given strings are equal or not. You can use != as not equal to operator. It returns false if string matches. Example In this example, we initialize two variable with sting and compare if both stings are equal or not.

| x = "Hello" y = "Hello" if x == y: print("Equal") else: print("Not equal") |

Output: Equal Another Example Here is another sample Python program which will prompt user to enter a strings. Then compare the input string with another predefined string.

| name = "Rahul" x = input("Enter your name to confirm: ") if x == name: print("Match found") fi |

Read More Python Program to Concatenate Two Strings Write a Python program to concatenate two string and print the result. Also create a example to take user input of two strings and concatenate and print the result. Using + symbol to concatenate two different strings together and get result string. Example A sample Python program to assgin static string to two variables and then concatenate. Then print the result string with print() function.

| str1 = "Hello" str2 = "TecAdmin" result = str1 + str2 print(result) |

Another example to take input from user and concatenate them.

| str1 = input("Enter first string: ") str2 = input("Enter second string: ") result = str1 + str2 print(result) |

Read More Working with Python input() Function The Python input() function is used to take input from standard input devices. With the help of this function, you can ask user to provide values to a program at run time. When a Python program is running and a input function is called. The program execution will wait until the user send a input and press return key. Syntax input(prompt) Example: Below sample Python program will print a message on screen. Then input() will ask your to enter your name.

| print("Enter your name: ") x = input() print('Welcome ' + x) |

Another example of…

Read More Python Program to Add Two Numbers Write a Python program take input of two numbers and calculate sum of two numbers. This program will prompt user to input two numbers on standard input. Then calculate sum of two numbers a print sum of both values on standards output. Example: The following Python program uses input() function to take input of 2 number from user. Calculate the sum and use print() function to print result.

| #A sample Python program to input two number #And calculate sum of both numbers #Take input of two numbers from user a = int(input("Enter first number: ")) b = int(input("Enter second number: ")) #Calculate sum sum = a + b #Print result print("Sum of input numbers is:", sum) |

Execute this program: Enter first number: 10 Enter second number: 5 Sum of input numbers…

Read More Python abs() Function The Python abs() method returns the absolute value of a number. It accept one argument of type integer or a floating point number. If the input argument is a complex number, its returns the magnitude. Example 1. Below is the sample Python program to calculate the absolute value of a integer and floating point number. Write the below script in a python script:

| # A sample python program to get absolute value of # an integer or floating point number num1 = -10 print abs(num1) num2 = -3.638 print abs(num2) |

Run the script and see the output: 10 3.638 Example 2. Here is another example of calculating absolute value of a complex number.…

Read More Python Strings A string is a sequence of characters and a character is can be a alphabet, digit or symbol. The Python treated any sequence of character’s enclosed in quotes as a string.

Python treated both single and double quted sequence as string. But in results of any string will be in a single quote only. like: ‘Welcome’ String Concatenation In Python strings have operation symbols too. Using a plus (+) symbol concatenate two strings. let’s type below example on Python shell.

| 'Hello ' + 'World' 'Hello ' + "World" |

The result will be same ‘Hello…

Read More