Found 31499 Articles for Programming

Python Program to Sort A List Of Names By Last Name

Vikram Chiluka
Updated on 27-Jan-2023 12:05:17
In this article, we will learn a python program to sort a list of names by last name. Methods Used Using lambda and sorted() functions Using lambda and sort() functions Example Assume we have taken an input list containing string elements. We will now sort the list elements in ascending order of the last names alphabetically using the above methods. Input Input List: ['sachin tendulkar', 'suresh raina', 'hardik pandya'] Output Input List after sorting by last names: ['hardik pandya', 'suresh raina', 'sachin tendulkar'] The last names of input list elements are tendulkar, raina, pandya i.e t, ... Read More

Python Program to Replace all words except the given word

Vikram Chiluka
Updated on 27-Jan-2023 12:03:24
In this article, we will learn how to replace all words except the given word in a string in python. Methods Used The following are the various methods to accomplish this task − Using For Loop, split() & join() Functions Using List Comprehension Example Assume we have taken an input string, input word, and the input character with which to be replaced. We will now replace all the words in an input string with the given input replace character expect the given word using the above methods. Input inputString = 'hello tutorialspoint python codes' inputWord = "tutorialspoint" ... Read More

Python Program to Remove numbers with repeating digits

Vikram Chiluka
Updated on 27-Jan-2023 12:02:20
In this article, we will learn how to remove numbers with repeating digits in python. Methods Used The following are the various methods to accomplish this task − Using list comprehension and set() function Using re module Using the Counter() function Example Assume we have taken an input list containing numbers. We will now remove all the numbers from the list containing repeating digits and return the resultant list using the above-mentioned methods. Input inputList = [3352, 8135, 661, 7893, 99] Output [8135, 7893] In the above input list, in the 1st element 3352, 3 ... Read More

Python program to remove K length words in String

Vikram Chiluka
Updated on 27-Jan-2023 12:00:36
In this article, we will learn a python program to remove K-length words in String. Methods Used The following are the various methods to accomplish this task − Using split(), join(), len() and list comprehension Using filter(), lambda(), split() and len() functions Using split(), join(), remove() and len() functions Example Assume we have taken an input string and k length. We will now remove all the given k-length words from the input string using the above methods. Input inputString = "hello tutorialspoint python codes" k_length = 5 Output Resultant string after removing 5 length words: tutorialspoint ... Read More

Python Program to Remove all digits before given Number

Vikram Chiluka
Updated on 27-Jan-2023 11:59:04
In this article, we will learn how to remove all digits before a given number in python. Methods Used The following are the various methods to accomplish this task − Using List Comprehension, split(), enumerate() & index() Functions Using re module Using slicing, index() & replace() functions Example Assume we have taken an input string and an input number. We will now remove all the digits before the input number from an input string. Input inputString = 'hello 6 8 10 tutorialspoint 15 python codes' inputNumber = 10 Output Resultant string after removing digits before input number{ ... Read More

Python Program to Random uppercase in Strings

Vikram Chiluka
Updated on 27-Jan-2023 11:57:31
In this article, we will learn how to convert characters to uppercase in Strings Randomly using python. Methods Used The following are the various methods to accomplish this task − Using join(), choice(), lower(), upper() functions. Using random.randInt() and random.sample() functions. Using map(), choice(), zip() functions Example Assume we have taken an input string. We will now randomly convert the characters of an input string to uppercase using the above methods. Input inputString = 'tutorialspoint' Output String after converting chars lower or uppercase randomly: tUToriaLsPOINT In this example, We are converting characters of an input ... Read More

Python Program to move numbers to the end of the string

Vikram Chiluka
Updated on 27-Jan-2023 11:54:14
In this article, we will learn a python program to move numbers to the end of the string. Methods Used The following are the various methods to accomplish this task − Using isdigit() and for loop Using isdigit() and join() functions Without using any built-in functions Using isnumeric() function Using isalpha() function Example Assume we have taken an input string. We will now move all the numbers contained in an input string to the end of the string using the above methods. Input inputString = 'hello5691 tutorialspoint1342' Output Resultant string after adding digits at the end: hello ... Read More

Python Program to Join strings by multiple delimiters

Vikram Chiluka
Updated on 27-Jan-2023 11:53:02
In this article, we will learn how to join strings by multiple delimiters in python. Methods Used The following are the various methods to accomplish this task − Using List Comprehension and “+” Operator Using List Comprehension and join() Function Example Assume we have taken two input strings and a multiple delimiters list. We will now join both the input strings with the given input delimiters and returns a resultant list using the above methods. Input Input String 1: hello Input String 2: tutorialspoint delimitersList = ["-", "^", "%", "$", "#"] Output Both the strings after joining ... Read More

Python Program to Get word frequency in percentage

Vikram Chiluka
Updated on 27-Jan-2023 11:51:40
In this article, we will learn how to get word frequency in percentage in python. Assume we have taken an input list of strings. We will now find the percentage of each word in the given input list of strings. Formula (Occurrence of X word / Total words) * 100 Methods Used Using sum(), Counter(), join() and split() functions Using join(), split() and count() functions Using countOf() function from operator module. Method 1: Using sum(), Counter(), join() and split() functions join() is a string function in Python that is used to join elements of a sequence ... Read More

Python program to find the sum of dictionary keys

Vikram Chiluka
Updated on 27-Jan-2023 11:49:49
In this article, we will learn a python program to find the sum of dictionary keys. Methods Used The following are the various methods to accomplish this task − Using for loop Using dict.keys() and sum() functions Using reduce() function Using items() function Example Assume we have taken an input dictionary containing some random key-value pairs. We will now find the sum of all the keys of an input dictionary using the above methods. Input inputDict = {4: 10, 1: 30, 10: 50, 2: 70, 8: 90} Output Sum of keys of an input dictionary: 25 ... Read More
Advertisements