Python – Find String Length

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:

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

Execute above Python script. enter a string when…

Read More

Python – Generate Random Number

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.

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 – Find Factorial of Number

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.

Output: The factorial of 5 is 120

Read More

Python – Print All Prime Numbers

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.

Ouput: Enter max number: 20…

Read More

Python – Check Even Odd Number

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

Python – Compare Two Strings

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.

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.

Read More

Python – Concatenate Two Strings

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.

Another example to take input from user and concatenate them.

Read More

Python input() Function

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.

Another example of…

Read More

Python – Add Two Numbers

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.

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

Read More

Python – Check Prime Number

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:

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

Read More