Found 10805 Articles for Python

Python program to find all duplicate characters in a string

Sarika Singh
Updated on 26-Aug-2023 08:14:18

31K+ Views

This article teaches you how to write a python program to find all duplicate characters in a string. Characters that repeat themselves within a string are referred to as duplicate characters. When we refer to printing duplicate characters in a string, we mean that we shall print every character, including spaces, that appears more than once in the string in question. Input-Output Scenarios Following is the input-output scenario to find all the duplicate characters in a string − Input: TutorialsPoint Output: t, o, i As we can see, the duplicate characters in the given string "TutorialsPoint" are "t" with ... Read More

Python program to find all close matches of input string from a list

Hafeezul Kareem
Updated on 27-Aug-2019 12:23:17

364 Views

In this tutorial, we are going to find a solution to a problem. Let's see what the problem is. We have a list of strings and an element. We have to find strings from a list in which they must closely match to the given element. See the example.Inputs strings = ["Lion", "Li", "Tiger", "Tig"] element = "Lion" Ouput Lion LiWe can achieve this by using the startswith built-in method. See the steps to find the strings.Initialize string list and a string.Loop through the list.If string from list startswith element or element startswith the string from the listPrint the stringExample## initializing ... Read More

Program to check if all the values in a list that are greater than a given value in Python

Hafeezul Kareem
Updated on 27-Aug-2019 12:12:11

667 Views

In this tutorial, we will check whether all the elements in a list are greater than a number or not. For example, we have a list [1, 2, 3, 4, 5] and a number 0. If every value in the list is greater than the given value then, we return True else False.It's a simple program. We write it in less than 3 minutes. Try it yourself first. If you are not able to find the solution, then, follow the below steps to write the program.Initialise a list and any numberLoop through the list.If yes, return **False**Return True.Example## initializing the list    values ... Read More

Permutation of a given string using the inbuilt function in Python

Hafeezul Kareem
Updated on 16-Apr-2021 19:44:10

3K+ Views

In this tutorial, we are going to find the permutation of a string using the inbuilt function of Python called permutations. The method permutations is present in the itertools module.Procedure To Find The Permutation Of A StringImport the itertools module.Initialize the string.Use the itertools.permutations method to find the permutation of the string.In the third step, the method returns an object and convert it into a list.List contains a permutation of string as tuples.ExampleLet's see the program.## importing the module import itertools ## initializing a string string = "XYZ" ## itertools.permutations method permutaion_list = list(itertools.permutations(string)) ## printing the obj in list print("-----------Permutations Of String ... Read More

Python eval()

Pradeep Elance
Updated on 23-Aug-2019 12:43:20

471 Views

The eval() method parses the expression passed on to this method and runs the expression within the program. In other words, it interprets a string as code inside a python program.SyntaxThe Syntax for eval is as below −eval(expression, globals=None, locals=None)WhereExpression − It is the python expression passed onto the method.globals − A dictionary of available global methods and variables.locals − A dictionary of available local methods and variables.In the below example we allow the user to cerate an expression and run a python program to evaluate that expression. So it helps in create dynamic code.Example Live Demo# expression to be evaluated ... Read More

How to split a string in Python

Pradeep Elance
Updated on 23-Aug-2019 12:38:50

318 Views

Manytimes we need to split a given string into multiple parts based on some delimiter. Python provides a function named split() which can be used to achieve this. It also provides a way to control the delimiter and number of characters to be considered as delimiter.ExampleIn the below example we a string containing many words and space in between. But there are two space characters between Banana and grape. Accordingly the split happens. When no parameter is supplied each space is taken as a delimiter. Live Demostr = "Apple Banana Grapes Apple"; print(str.split()) print(str.split(' ', 2))OutputRunning the above code gives us ... Read More

Help function in Python

Pradeep Elance
Updated on 23-Aug-2019 12:31:41

144 Views

Many times we need to look into the python documentation for some help on functions, modules etc. Python provides a help function that gives us this needed results.SyntaxHelp(‘term’) Where term is the word on which we want the help.ExampleIn the below example we seek to find help on the word time. The output comes from python documentation and it is quite exhaustive. Live Demoprint(help('time'))OutputRunning the above code gives us the following result −Help on built-in module time: NAME time - This module provides various functions to manipulate time values. DESCRIPTION There are two standard representations of time. One is the ... Read More

Filter in Python

Pradeep Elance
Updated on 23-Aug-2019 12:29:22

298 Views

We sometimes arrive at a situation where we have two lists and we want to check whether each item from the smaller list is present in the bigger list or not. In such case we use the filter() function as discussed below.SyntaxFilter(function_name, sequence name)Here Function_name is the name of the function which has the filter criteria. Sequence name is the sequence which has elements that needs to be filtered. It can be sets, lists, tuples, or other iterators.ExampleIn the below example we take a bigger list of some month names and then filter out those months which does not have ... Read More

factorial() in Python

Pradeep Elance
Updated on 23-Aug-2019 12:26:17

6K+ Views

Finding the factorial of a number is a frequent requirement in data analysis and other mathematical analysis involving python. The factorial is always found for a positive integer by multiplying all the integers starting from 1 till the given number. There can be three approaches to find this as shown below.Using a For LoopWe can use a for loop to iterate through number 1 till the designated number and keep multiplying at each step. In the below program we ask the user to enter the number and convert the input to an integer before using it in the loop. This ... Read More

exec() in Python

Pradeep Elance
Updated on 23-Aug-2019 12:15:41

893 Views

Exec function can dynamically execute code of python programs. The code can be passed in as string or object code to this function. The object code is executed as is while the string is first parsed and checked for any syntax error. If no syntax error, then the parsed string is executed as a python statement.Syntax for exec() Functionexec(object, globals, locals)WhereObject − A string or a code object passed onto the method.globals − A dictionary of available global methods and variables.locals − A dictionary of available local methods and variables.Passing StringIn the below example we pass a single line of ... Read More

Advertisements