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 Python Program to Check if a Number is Even or Odd Q. Write a Python program to to check if a given number is a even number or odd number. What is even and odd number? If a number is divided by 2 and remainder is 0, then the given number is even number, else the number is a odd number. Even numbers are – 2, 4, 6, 8, 10 … Odd numbers are – 1, 3, 5, 7, 9 … Example An example to check if given number is…

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 Program to Check Prime Number Write a Python program to check if a given number is a prime number or not. A prime number is a number that is fully divisible by itself or 1 only. Such as 13 is a prime number, it is not fully divisible by any of the number between 2-12. Similarily 3, 7, and 11 etc are the prime numbers Example: Here is a sample Python program:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | # Python program to check if given number is prime number or not num = 13 # Uncomment below to take user input # num = int(input("Enter a number: ")) # prime numbers are greater than 1 if num > 1: for i in range(2,num): if (num % i) == 0: print(num, "is not a prime number") break else: print(num, "is a prime number") else: print(num, "is not a prime number") |

Execute above script and you will see the result like: 13 is a prime number

Read More