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 – 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 abs() Function

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:

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

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.

The result will be same ‘Hello…

Read More