Found 10476 Articles for Python

Python Program to Subtract K from each digit

Ranbir Kapoor
Updated on 31-Jul-2023 12:39:55

114 Views

In this article, we will learn a python program to subtract K from each digit in a list. Assume we have taken an input list containing numbers and K value. We will now subtract k from each digit of a list element and print the resultant list using the below methods. Example Let inputList = [1034, 356, 2145, 6712, 8671] Given input k value = 3 Now we consider 1st element i.e, 1034 Subtract 3 from each digit of 1034 i.e, 1-3 =-2. So, it is ceiled to 0 0-3 = -3. So, it is ceiled to ... Read More

Python Program to Split a String by Custom Lengths

Vikram Chiluka
Updated on 27-Jan-2023 12:08:51

3K+ Views

In this article, we will learn how to split a string by custom lengths in python. Methods Used The following are the various methods to accomplish this task − Using for loop and slicing Using List Comprehension, join() and next() functions Example Assume we have taken an input string and custom lengths list. We will now split an input string by input custom lengths using the above methods. Input inputString = 'hitutorialspoint' customLengths = [4, 1, 6, 5] Output ['hitu', 't', 'orials', 'point'] In the above example, firstly the input list is split based on the ... Read More

Python Program to Sort A List Of Names By Last Name

Vikram Chiluka
Updated on 27-Jan-2023 12:05:17

2K+ Views

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

358 Views

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

929 Views

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

575 Views

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

323 Views

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

1K+ Views

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

784 Views

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

501 Views

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

Advertisements